-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathBinarySearchOnRotatedSortedArray.cpp
More file actions
44 lines (40 loc) · 1.11 KB
/
BinarySearchOnRotatedSortedArray.cpp
File metadata and controls
44 lines (40 loc) · 1.11 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
37
38
39
40
41
42
43
44
//This is the algorithm for binary search if your arr is initially sorted and then rotated by some places
//Example search 0 in [4,5,6,7,0,1,2]
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int search(vector<int> &nums, int target)
{
return find(nums, target, 0, nums.size() - 1);
}
int find(vector<int> &nums, int target, int start, int end)
{
if (start > end)
return -1;
int mid = (start + end) / 2;
if (nums[mid] == target)
return mid;
if (nums[mid] < nums[end])
{
if (target > nums[mid] and target <= nums[end])
return find(nums, target, mid + 1, end);
return find(nums, target, start, mid - 1);
}
else
{
if (target >= nums[start] and target < nums[mid])
return find(nums, target, start, mid - 1);
return find(nums, target, mid + 1, end);
}
}
};
int main()
{
vector<int> nums = {4, 5, 6, 7, 0, 1, 2};
int target = 0;
Solution s;
cout << s.search(nums, target);
return 0;
}