forked from SR-Sunny-Raj/Hacktoberfest2021-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8. Search in Rotated Sorted Array.cpp
More file actions
36 lines (29 loc) · 1 KB
/
8. Search in Rotated Sorted Array.cpp
File metadata and controls
36 lines (29 loc) · 1 KB
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
class Solution {
public:
int search(vector<int>& nums, int target) {
if(nums.size() == 0) return -1;
int left = 0;
int right = nums.size() - 1;
while(left < right)
{
int mid = left + (right-left)/2;
if(nums[mid] > nums[right]) left = mid + 1;
else right = mid;
}
int start = left; // lowest index or the pivot point
left = 0;
right = nums.size() - 1;
if(target >= nums[start] && target <= nums[right])
left = start;
else right= start;
while (left <= right)
{
int midpoint = left + (right-left)/2;
if(nums[midpoint] == target)return midpoint;
else if (nums[midpoint] < target) left = midpoint + 1;
else right = midpoint - 1;
}
return -1;
}
};
//https://leetcode.com/problems/search-in-rotated-sorted-array/