forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchRange.java
More file actions
52 lines (40 loc) · 1 KB
/
SearchRange.java
File metadata and controls
52 lines (40 loc) · 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package BinarySearch;
import java.util.ArrayList;
import java.util.List;
/**
* Author - archit.s
* Date - 03/10/18
* Time - 12:08 PM
*/
public class SearchRange {
int bSearch(List<Integer> a, int b, boolean searchFirst){
int low = 0;
int high = a.size()-1;
int res = -1;
while(low <= high){
int mid = low + (high - low)/2;
if(a.get(mid) == b){
res = mid;
if(searchFirst){
high = mid-1;
}
else{
low = mid+1;
}
}
else if(a.get(mid) < b){
low = mid+1;
}
else{
high = mid-1;
}
}
return res;
}
public ArrayList<Integer> searchRange(final List<Integer> a, int b) {
ArrayList<Integer> ans = new ArrayList<>();
ans.add(bSearch(a,b,true));
ans.add(bSearch(a,b,false));
return ans;
}
}