-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotateCount.java
36 lines (30 loc) · 940 Bytes
/
RotateCount.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
package practice;
public class RotateCount {
public static void main(String[] args) {
int [] ar={0,1,2,3};
System.out.println(RotateCounting(ar) + " times rotated.") ;
//int p= PivotCount(ar)+1;
// System.out.println("rotate count: " + p);
}
public static int RotateCounting(int []arr){
int p=PivotCount(arr);
return p+1;
}
public static int PivotCount(int[] arr){
int start=0;
int end=arr.length-1;
while(start<=end) {
int mid = start + (end - start) / 2;
if ( mid<end && arr[mid] > arr[mid + 1]) {
return mid;
} else if (start<mid && arr[mid] < arr[mid - 1]) {
return mid - 1;
} else if (arr[start] <= arr[mid]) {
start = mid + 1;
} else if (arr[start] > arr[mid]) {
end = mid - 1;
}
}
return -1;
}
}