-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfiniteArray.java
More file actions
31 lines (31 loc) · 985 Bytes
/
Copy pathInfiniteArray.java
File metadata and controls
31 lines (31 loc) · 985 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
public class InfiniteArray {
public static void main(String[] args) {
int[] nums = {1, 2, 4, 5, 7, 8, 13, 15, 23, 28, 30, 32, 35, 37, 45, 50};
int target = 23;
int ans = ans(nums, target);
System.out.println(ans);
}
static int binarySearch(int[] nums, int target, int start, int end){
while(start <= end){
int mid = start + (end - start) / 2;
if(target < nums[mid]){
end = mid - 1;
}else if(target > nums[mid]){
start = mid + 1;
}else{
return mid;
}
}
return -1;
}
static int ans(int[] nums, int target){
int start = 0;
int end = 1;
while(target > nums[end]){
int newStart = end + 1;
end = end + (end - start + 1) * 2;
start = newStart;
}
return binarySearch(nums, target, start, end);
}
}