-
Notifications
You must be signed in to change notification settings - Fork 12
/
RotationCount.java
47 lines (45 loc) · 1.17 KB
/
RotationCount.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
36
37
38
39
40
41
42
43
44
45
46
47
package com.company.Property;
public class RotationCount {
public static void main(String[] args) {
int arr[]={7, 9, 11, 12, 15};
int pivot=findPivot(arr);
System.out.println(pivot+1);
}
static int findPivot(int arr[]){
int start=0;
int end=arr.length-1;
int mid;
while (start<=end){
mid=start+(end-start)/2;
if(mid<end && arr[mid]>arr[mid+1]){
return mid;
}
if(mid>start && arr[mid]<arr[mid-1]){
return mid-1;
}
if(arr[start]<arr[mid]){
start=mid+1;
}
else {
end=mid-1;
}
}
return -1;
}
static int binarySearch(int nums[],int start,int end,int target){
int mid;
while(start<=end){
mid=start+(end-start)/2;
if (nums[mid]==target){
return mid;
}
else if (nums[mid]<target){
start=mid+1;
}
else{
end=mid-1;
}
}
return -1;
}
}