forked from super30admin/Binary-Search-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch_infinite_array.java
More file actions
35 lines (32 loc) · 1.11 KB
/
Search_infinite_array.java
File metadata and controls
35 lines (32 loc) · 1.11 KB
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
/*
Time Complexity : O(log(N)) N = position where target is
Space Complexity : O(1) No Auxilary Space is used
is Tested on Leetcode : No because it is locked, I dont have premium subscription
Difficulty faced : No I find it easier after class discussion
Technique I learned : Binary Search is not all about devide array in two half,
you can also use expansion technique
*/
public class Search_infinite_array {
public int search(ArrayReader reader, int target){
int left = 0;
int right =1;
// find upper limit for the element
while(reader.get(right) < target){
left = right;
right = right*2;
}
return binarySearch(reader,target, left,right);
}
int binarySearch(ArrayReader reader, int target, int left, int right){
while(left <= right){
mid = left + (right-left)/2;
if (reader.get(mid) == target) return mid;
else if(reader.get(mid) > target){
right = mid -1;
}else{
left = mid +1;
}
}
return -1;
}
}