-
Notifications
You must be signed in to change notification settings - Fork 101
/
Infinitesortedarray.java
41 lines (34 loc) · 1.05 KB
/
Infinitesortedarray.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
41
import java.util.Scanner;
class FindElementInSortedInfiniteArray {
private static int binarySearch(int[] a, int target) {
int start = 0;
int end = 1;
// First try to find the lower and upper bounds before applying binary search
while (target > a[end]) {
start = end;
end *= 2;
}
while (start <= end) {
int mid = (start + end) / 2;
if (target == a[mid]) {
return mid;
} else if (target < a[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
return -1;
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int n = keyboard.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = keyboard.nextInt();
}
int target = keyboard.nextInt();
keyboard.close();
System.out.printf("BinarySearch(%d) = %d%n", target, binarySearch(a, target));
}
}