-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathInfiniteArray.java
More file actions
32 lines (31 loc) · 997 Bytes
/
InfiniteArray.java
File metadata and controls
32 lines (31 loc) · 997 Bytes
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
package com.binarysearch;
public class InfiniteArray {
public static void main(String[] args) {
int[] arr = {2,3,5,6,7,8,9,12,14,15,17,46,78,98,122,123,435,2334,3466,24456};
int target = 78;
System.out.println(Result(arr,target));
}
static int Result(int[] arr, int target){
int start = 0;
int end = 1;
while ( target > arr[end] ){
int newStart = start;
end = end + (end - start + 1)*2;
start = newStart;
}
return binarySearch(arr,target,start,end);
}
static int binarySearch(int[] arr, int target, int start, int end) {
while (start <= end) {
int mid = end + ((start - end) / 2);
if (arr[mid] < target) {
start = mid + 1;
} else if (arr[mid] > target) {
end = mid - 1;
} else {
return mid;
}
}
return -1;
}
}