-
Notifications
You must be signed in to change notification settings - Fork 17
/
InfiniteSorted.java
40 lines (31 loc) · 922 Bytes
/
InfiniteSorted.java
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
public class Main {
public static void main(String args[]) {
int arr[] = {-5, 14, 20, 23, 42, 49, 52, 78};
System.out.println(infiniteSorted(arr, 52));
}
static int infiniteSorted(int arr[], int key) {
int low = 0;
int high = 1;
while(low <= high) {
int mid = (low+high) / 2;
if(arr[mid] == key) return mid;
if(arr[mid] < key) {
low = high;
high = 2 * high;
}
}
return binarySearch(arr, key, low, high);
}
static int binarySearch(int arr[], int key, int low, int high) {
while(low <= high) {
int mid = (low+high)/2;
if(arr[mid] == key) return mid;
else if(arr[mid] > key) {
high = mid-1;
} else {
low = mid+1;
}
}
return -1;
}
}