-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch_In_Rotated_Sorted_Array-Ankit_Tiwari.cpp
56 lines (53 loc) · 1.29 KB
/
Search_In_Rotated_Sorted_Array-Ankit_Tiwari.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
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
int search(vector<int>& nums, int target) {
int low = 0;
int high = nums.size() - 1;
while(low <= high)
{
int mid = (low + high) / 2;
if(target == nums[mid]) return mid;
if(nums[low] <= nums[mid])
{
if(nums[low] <= target && target <= nums[mid])
high = mid - 1;
else
low = mid + 1;
}
else
{
if(nums[mid] <= target && target <= nums[high])
low = mid + 1;
else
high = mid - 1;
}
}
return -1;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
cin.ignore();
vector<int> arr;
string input;
getline(cin, input);
stringstream ss(input);
int number;
while (ss >> number) {
arr.push_back(number);
}
int key;
cin >> key;
Solution ob;
cout << ob.search(arr, key) << endl;
}
return 0;
}
// } Driver Code Ends