forked from super30admin/Binary-Search-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchSortedArrayUnknownSize.java
48 lines (40 loc) · 1.73 KB
/
SearchSortedArrayUnknownSize.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
/*
Time Complexity: O(log m), where m is the index at which the target would be or the nearest higher value.
Space Complexity: O(1)
Did this code successfully run on Leetcode: Yes
Here we are increasing the search space by twice which is also Binary search. We can also use any other number to multiply, but the only thing is
we will be doing binary search on more elements
*/
/**
* // This is ArrayReader's API interface.
* // You should not implement it, or speculate about its implementation
* interface ArrayReader {
* public int get(int index) {}
* }
*/
class SearchSortedArrayUnknownSize {
public int search(ArrayReader reader, int target) {
int low = 0, high = 1;
/*We want find the range so we make the high pointer jump by 2, we can also make high jump by a high number, say high = max value 2^31, if we do this, the number of iterations
to get the range (log m) will be less, and if we have to do another binary search to identify the target, (log k), so even though we get the range quicker, during binary search
we wlll be have more number of elements, as we are going in more depth, that will make the time complexity as O(log infinity) which we don't want.
*/
//While loop to identify the range to perform search
while(reader.get(high) < target) {
low = high;
high = high * 2;
}
//Now we have the range so perform binary search
while(low<=high)
{
int mid = (low+(high-low)/2);
if(reader.get(mid) == target)
return mid;
else if(reader.get(mid) > target)
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
}