-
Notifications
You must be signed in to change notification settings - Fork 3
/
Number of Rotations.java
62 lines (49 loc) · 1.35 KB
/
Number of Rotations.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// { Driver Code Starts
//Initial Template for Java
import java.io.*;
import java.util.*;
class GFG{
public static void main(String args[]) throws IOException {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t > 0){
int n = sc.nextInt();
int array[] = new int[n];
for (int i = 0; i < n; ++i)
{
array[i] = sc.nextInt();
}
Solution ob = new Solution();
System.out.println(ob.NumberOfRotations(array,n));
t--;
}
}
} // } Driver Code Ends
//User function Template for Java
class Solution
{
static int NumberOfRotations(int arr[], int n)
{
// code here
int start =0;
int end = n-1;
int mid = 0;
int ans = 0;
while(start<end)
{
if(start==end)
return start;
mid = (end+start)/2;
if (mid < end && arr[mid+1] < arr[mid])
return (mid + 1);
if (mid > start && arr[mid] < arr[mid - 1])
return mid;
if(arr[start]>arr[mid])
end = mid-1;
else if(arr[mid]>arr[end])
start = mid+1;
else return start;
}
return ans;
}
}