forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateArray.java
More file actions
46 lines (35 loc) · 913 Bytes
/
RotateArray.java
File metadata and controls
46 lines (35 loc) · 913 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
package BinarySearch;
import java.util.List;
/**
* Author - archit.s
* Date - 02/10/18
* Time - 10:36 AM
*/
public class RotateArray {
int rotateBSearch(List<Integer> A){
int low = 0;
int high = A.size()-1;
int n = A.size();
while(low <= high){
int mid = low + (high - low)/2;
int next = (mid+1)%n;
int prev = (mid-1+n)%n;
if(A.get(low) <= A.get(high)){
return A.get(low);
}
else if((A.get(mid) <= A.get(next)) && (A.get(mid) <= A.get(prev))){
return A.get(mid);
}
else if(A.get(mid) >= A.get(low)){
low = mid + 1;
}
else {
high = mid - 1;
}
}
return -1;
}
public int findMin(final List<Integer> a) {
return rotateBSearch(a);
}
}