-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfiniteLengthArray.java
More file actions
39 lines (31 loc) · 961 Bytes
/
Copy pathInfiniteLengthArray.java
File metadata and controls
39 lines (31 loc) · 961 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
33
34
35
36
37
38
public class InfiniteLengthArray {
public static void main(String []args){
int array[] = {10,191,200,409,413,777,800};
int target = 10;
int result = find(array,target);
System.out.println(result);
}
public static int find(int array[],int target){
int start = 0 ;
int end = 1 ;
while(target>array[end]){
start = end +1;
end = end + (end-start+1)*2;
}
return search(array, target, start, end);
}
public static int search(int array[], int target , int start , int end){
int ans = -1;
// int start = 0 ;
// int end = array.length-1;
while(start<=end){
int mid = start + (end-start)/2;
if(array[mid]>target) end = mid-1;
else if(array[mid]<target) start = mid+1;
else{
ans = mid;
}
}
return ans;
}
}