-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPairInSortedRotated.java
More file actions
46 lines (39 loc) · 1.28 KB
/
PairInSortedRotated.java
File metadata and controls
46 lines (39 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package Array;
public class PairInSortedRotated {
/**
* @param arr - It is the array under consideration.
* @param n - It is the length of the array.
* @param x - It is the sum of the pair which is to be found.
* @return - It returns, if the pair is present or not in the array 'arr' whose
* sum is {x}.
*/
static boolean pairInSortedRotated(int[] arr, int n, int x) {
int i;
for (i = 0; i < n - 1; i++)
if (arr[i] > arr[i + 1])
break;
// Smallest element Index
int smallest = (i + 1) % n;
// Largest element Index
int largest = i;
while (smallest != largest) {
if (arr[smallest] + arr[largest] == x)
return true;
if (arr[smallest] + arr[largest] < x)
smallest = (smallest + 1) % n;
else
largest = (n + largest - 1) % n;
}
return false;
}
// Time Complexity - O(n), Auxiliary Space - O(1)
public static void main(String[] args) {
int[] arr = {11, 15, 6, 8, 9, 10};
int X = 16;
int N = arr.length;
if (pairInSortedRotated(arr, N, X))
System.out.print("True");
else
System.out.print("False");
}
}