-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathinfinity_array.java
49 lines (47 loc) · 1.73 KB
/
infinity_array.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
42
43
44
45
46
47
48
49
/*Binary Search in an Infinite array in Java
* Given an array whose size is not fixed,find the index of the target element in the array.
* EXAMPLE: [5,15,34,56,77,87...] (array can be of any size)
* Target = 56;
* Output: 3;
*
* APPROACH:
* The approach will be similar to normal binary search but in reverse order
* ie; For the start and end value, find the range which will be taking the target in the range.
* We will not specify the array length since the array size is infinity.
* Send the start and end value to the search function and return the position of the element.
*
*/
public class InfinityArray {
public static void main(String[] args){
int[] nums = {2,5,7,9,14,45,56,77,89,101};// can be of any size
int target = 56;
System.out.println(range(nums,target));
}
public static int range(int[] nums,int target){
int start=0,end=1;
while(target > nums[end]){
int temp = end +1;
end = end + (end-start)*2;
start = temp;
}
return search(nums,target,start,end);
}
public static int search(int[] nums, int target,int start,int end) {
while(start<=end){
int mid = start + (end-start)/2;
// if middle value is equal to target return the mid index
if(nums[mid] == target){
return mid;
}
// if mid value is greater than the target, search the left sub array
else if(nums[mid] > target){
end = mid - 1;
}
// if mid value is lesser than the target, search the right sub array
else if(nums[mid] < target){
start = mid + 1;
}
}
return -1;
}
}