forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotatedArraySearch.java
More file actions
50 lines (40 loc) · 1001 Bytes
/
RotatedArraySearch.java
File metadata and controls
50 lines (40 loc) · 1001 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
39
40
41
42
43
44
45
46
47
48
49
50
package BinarySearch;
import java.util.List;
/**
* Author - archit.s
* Date - 03/10/18
* Time - 12:02 PM
*/
public class RotatedArraySearch {
int bSearch(List<Integer> a, int b){
int low = 0;
int high = a.size()-1;
int n = a.size();
while(low<=high){
int mid = low + (high-low)/2;
if(a.get(mid) == b){
return mid;
}
else if(a.get(low) <= a.get(mid)){
if(b >= a.get(low) && b<= a.get(mid)){
high = mid -1;
}
else{
low = mid+1;
}
}
else{
if(b <= a.get(high) && b >= a.get(mid)){
low = mid+1;
}
else{
high = mid -1;
}
}
}
return -1;
}
public int search(final List<Integer> a, int b) {
return bSearch(a,b);
}
}