-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find kth rotation
57 lines (44 loc) · 1.39 KB
/
Find kth rotation
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
//Find kth rotation
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int t = Integer.parseInt(sc.nextLine());
while(t-- > 0) {
List<Integer> arr = new ArrayList<>();
String input1 = sc.nextLine();
Scanner ss1 = new Scanner(input1);
while(ss1.hasNextInt()) {
arr.add(ss1.nextInt());
}
Solution ob = new Solution();
int res = ob.findKRotation(arr);
System.out.println(res);
}
}
}
class Solution {
public int findKRotation(List<Integer> arr) {
int left = 0, right = arr.size() - 1, ind = 0;
int min = Integer.MAX_VALUE;
while(left <= right) {
int mid = (left + right) / 2;
if(arr.get(left) <= arr.get(mid)) {
min = Math.min(arr.get(left), min);
if(min == arr.get(left)) {
ind = left;
}
left = mid + 1;
}
else {
min = Math.min(arr.get(mid), min);
if(min == arr.get(mid)) {
ind = mid;
}
right = mid - 1;
}
}
return ind;
}
}