-
Notifications
You must be signed in to change notification settings - Fork 5
/
LC_33_SearchRotatedSortedArray.cpp
86 lines (70 loc) · 2.33 KB
/
LC_33_SearchRotatedSortedArray.cpp
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
https://leetcode.com/problems/search-in-rotated-sorted-array/
33. Search in Rotated Sorted Array
*/
class Solution {
public:
int search(vector<int>& nums, int target) {
int n = nums.size();
if(n == 0) return -1;
// if array is already sorted without rotation
if(nums[0]<=nums[n-1])
return binary_search(nums, 0, n-1, target);
int pivot_index = findPivotElementIndex(nums, 0, n-1);
if(nums[pivot_index]==target)
return pivot_index; // if pivot index element itself is the target
// else if (target > nums[pivot_index] && target <=nums[n-1])
// return binary_search(nums, pivot_index+1, n-1, target); // search in right array
// else
// return binary_search(nums, 0, pivot_index-1, target); // search in left array
int i =0;
int j = n-1;
if (target > nums[pivot_index] && target <=nums[n-1])
i = pivot_index+1;
else{
j=pivot_index-1;
}
int mid = -1;
while(i<=j)
{
mid = i + (j-i)/2;
if(nums[mid]==target)
return mid;
else if(nums[mid] < target)
i=mid+1;
else
j=mid-1;
}
return -1;
}// end
int binary_search(vector<int>&nums, int i, int j, int target){
int mid = -1;
while(i<=j)
{
mid = i + (j-i)/2;
if(nums[mid]==target)
return mid;
else if(nums[mid] < target)
i=mid+1;
else
j=mid-1;
}
return -1;
}
int findPivotElementIndex(vector<int>& nums, int i, int j){
int mid = -1;
while(i<j)
{
mid = i + (j-i)/2;
// [4,5,6,7,0,1,2] // first IF find index 4, second IF index 4
// [0,1,2,4,5,6,7] // IF index 6, second IF index 0
// if(nums[0]<=nums[mid])
if(nums[mid] > nums[j]) // finding smallest element in the array index
i=mid+1;
else
j=mid;
// cout<<" i"<<i<<" ,j"<<j<<": ";
}
return i;
}//end
};