-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday3.cpp
More file actions
50 lines (42 loc) · 1.27 KB
/
day3.cpp
File metadata and controls
50 lines (42 loc) · 1.27 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
/*
An sorted array of integers was rotated an unknown number of times.
Given such an array, find the index of the element in the array in faster than linear time.
If the element doesn't exist in the array, return null.For example,
given the array [13, 18, 25, 2, 8, 10] and the element 8, return 4 (the index of 8 in the array).
You can assume all the integers in the array are unique.
[a,b,c,d,...,n]
*/
#include<iostream>
#include<vector>
using namespace std;
int searchRotated(const vector<int>& nums, int target){
// Modified binary search on rotated sorted array with unique elements
int left = 0;
int right = static_cast<int>(nums.size()) - 1;
while (left <= right){
int mid = left + (right - left) / 2;
if (nums[mid] == target) return mid;
// Left half is sorted
if (nums[left] <= nums[mid]){
if (nums[left] <= target && target < nums[mid]){
right = mid - 1;
}else{
left = mid + 1;
}
}else{ // Right half is sorted
if (nums[mid] < target && target <= nums[right]){
left = mid + 1;
}else{
right = mid - 1;
}
}
}
return -1; // not found
}
int main(){
vector<int> nums = {13, 18, 25, 2, 8, 10};
int target = 8;
int idx = searchRotated(nums, target);
cout << idx << endl;
return 0;
}