As it is rotated sorted array so i just made it unrotated sorted array that means i just sort the whole array by using inbuilt sorting system in Java. If you want you can do this sorting by using mergeSort or further better algorithm.
Then I create two pointer start and end which indicate the first position and last position of the array and i compare the sum of this two pointer with the targetted value. If targetted value is small then decrement the end by one else increment the start by one. If targetted value is equal to it's sum then return true. else the loop will iterate until start is greater than or equal to end. if it not return inside the loop it will return false as it came outside from that loop
The overall time complexity of the code is O(n log n) due to the sorting operation.
As for space complexity, the code uses only a constant amount of extra space for variables like start, end, and sum, regardless of the size of the input array. So, the space complexity is O(1), indicating constant space usage. Please upvote me.
import java.util.*;
public class Solution {
public static boolean findPairSum(int[] arr, int target) {
Arrays.sort(arr);
int start = 0;
int end = arr.length - 1;
while (start < end) {
int sum = arr[start] + arr[end];
if (sum == target) {
return true;
} else if (sum < target) {
start++;
} else {
end--;
}
}
return false;
}
}