-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind_Kth_rotation.cpp
63 lines (57 loc) · 1.35 KB
/
Find_Kth_rotation.cpp
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
63
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function template for C++
class Solution {
public:
int findKRotation(vector<int> &nums) {
int low = 0;
int high = nums.size() - 1;
int mini = INT_MAX;
int index = 0;
while(low <= high)
{
int mid = (low + high) / 2;
if(nums[low] <= nums[mid])
{
if(nums[low] < mini)
{
mini = nums[low];
index = low;
}
low = mid + 1;
}
else
{
if(nums[mid] < mini)
{
mini = nums[mid];
index = mid;
}
high = mid - 1;
}
}
return index;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
cin.ignore();
while (t--) {
vector<int> arr;
string input1;
getline(cin, input1);
stringstream ss1(input1);
int number1;
while (ss1 >> number1) {
arr.push_back(number1);
}
Solution ob;
int res = ob.findKRotation(arr);
cout << res << endl;
}
}
// } Driver Code Ends