-
Notifications
You must be signed in to change notification settings - Fork 9
/
Search in a Sorted Matrix.cpp
79 lines (61 loc) · 2.23 KB
/
Search in a Sorted Matrix.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
// PROBLEM :- https://leetcode.com/problems/search-in-rotated-sorted-array/
// LEETCODE | MEDIUM | BINARY SEARCH
// Observation 1 :- Any rotated sorted array can be divided into 2 sorted array.
// Find that pivot point.
// Observation 2 :- Pivot element A[pivot - 1] > A[pivot].
// Rest all elemeents are A[x - 1] < A[x].
// Use this to simulate B.S. break condition.
// How to reduce search space?
// Pivot element will be the smallest element of the whole array.
// compare A[mid] with A[0], reduce search space accordingly.
// Once Pivot is found, binary search on the basis of the proximity of the element to either left or right of Pivot.
// Edge case :- What if array is not rotated!!
class Solution {
public:
int binarySearch(vector<int>& nums, int low, int high, int target){
while(low <= high){
int mid = (low + high) >> 1;
if(nums[mid] == target){
return mid;
}else if(nums[mid] > target){
high = mid - 1;
}else{
low = mid + 1;
}
}
return -1; // not present
}
int search(vector<int>& nums, int target) {
int n = nums.size();
if(nums[0] <= nums[n - 1]){
// not rotated
return binarySearch(nums, 0, n - 1, target);
}
int low = 0;
int high = n - 1;
int rotatedIndex = -1;
while(low <= high){
int mid = (low + high) >> 1;
if(mid == 0){
break;
}else if(nums[mid] < nums[mid - 1]){
rotatedIndex = mid;
break;
}else if(nums[mid] < nums[0]){
high = mid - 1;
}else{
low = mid + 1;
}
}
if(rotatedIndex == -1){
rotatedIndex = high;
}
if(target < nums[0]){
// search in right hand side of pivot
return binarySearch(nums, rotatedIndex, n - 1, target);
}else{
// search in left hand side of pivot
return binarySearch(nums, 0, rotatedIndex - 1, target);
}
}
};