forked from super30admin/Binary-Search-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem3.java
More file actions
37 lines (31 loc) · 1.1 KB
/
Problem3.java
File metadata and controls
37 lines (31 loc) · 1.1 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
36
37
// Time Complexity : O(logn)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : I initially used wrong while condition in the first loop
// Your code here along with comments explaining your approach in three sentences only
/*
* We start with low = 0 and high = 1
* in the first while loop, we keep on doubling our search space and the terminating condition would be when target becomes larger than the high value
* Once we know the search space, we apply regular binary search
*/
public class Problem3 {
public int search(ArrayReader reader, int target) {
int low = 0;
int high = low+1;
while(reader.get(high) < target) {
low = high;
high = 2 * high;
}
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;
}
}