-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search_in_sorted_rotated_array.cpp
More file actions
56 lines (51 loc) · 1.26 KB
/
binary_search_in_sorted_rotated_array.cpp
File metadata and controls
56 lines (51 loc) · 1.26 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
45
46
47
48
49
50
51
52
53
54
55
56
#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <unordered_map>
#include <vector>
using namespace std;
int findElementIteratve(const vector<int> &input, int start, int end, int key) {
while (start <= end) {
int mid = (start + end) / 2;
if (input[mid] == key)
return mid;
else if (input[start] <= input[mid]) {
/*Left sub array is sorted, check if
key is with A[start] and A[mid] */
if (input[start] <= key && key <= input[mid]) {
/*
Key lies with left sorted part of array
*/
end = mid - 1;
} else {
/*
Key lies in right subarray
*/
start = mid + 1;
}
} else {
/*
In this case, right subarray is already sorted and
check if key falls in range A[mid+1] and A[end]
*/
if (input[mid + 1] <= key && key <= input[end]) {
/*
Key lies with right sorted part of array
*/
start = mid + 1;
} else {
/*
Key lies in left subarray
*/
end = mid - 1;
}
}
}
return -1;
}
int main() {
vector<int> vec{9, 9, 9, 9, 1, 2};
cout << boolalpha << findElementIteratve(vec, 0, vec.size() - 1, 9) << '\n';
return 0;
}