-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotated_array.cpp
More file actions
40 lines (36 loc) · 1.18 KB
/
Copy pathRotated_array.cpp
File metadata and controls
40 lines (36 loc) · 1.18 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
#include <iostream>
#include <vector>
using namespace std;
// Funstion to find the pivot index in a rotated sorted array
int main(){
vector<int> arr = {4, 5, 6, 7, 0, 1, 2};
int n = arr.size();
int s=0, e=n-1;
int mid;
int target = 0; // Example target value to search for
while(s <= e){
mid= s + (e - s) / 2;
if(arr[s] <= arr[mid-1]){
// If the left part is sorted
if(arr[s] <= target && target <= arr[mid-1]){
e = mid - 1; // Search in the left part
} else {
s = mid+1; // Search in the right part
}
}
if(arr[mid+1] <= arr[e]){
// If the right part is sorted
if(arr[mid+1] <= target && target <= arr[e]){
s = mid + 1; // Search in the right part
} else {
e = mid - 1; // Search in the left part
}
}
if(arr[mid] == target) {
// If the array is not rotated or has only one element
cout << "Element found at index: " << mid << endl;
return 0; // Element found
}
}
return -1; // Element not found
}