|
| 1 | +/* |
| 2 | +There is an integer array nums sorted in ascending order (with distinct values). |
| 3 | +
|
| 4 | +Prior to being passed to your function, nums is rotated at an unknown pivot index k (0 <= k < nums.length) such that |
| 5 | +the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). |
| 6 | +For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2]. |
| 7 | +
|
| 8 | +Given the array nums after the rotation and an integer target, return the index of target if it is in nums, or -1 |
| 9 | +if it is not in nums. |
| 10 | +
|
| 11 | +You must write an algorithm with O(log n) runtime complexity. |
| 12 | +
|
| 13 | +Input: nums = [4,5,6,7,0,1,2], target = 0 |
| 14 | +Output: 4 |
| 15 | +
|
| 16 | +Constraints: |
| 17 | + 1 <= nums.length <= 5000 |
| 18 | + -104 <= nums[i] <= 104 |
| 19 | + All values of nums are unique. |
| 20 | + nums is guaranteed to be rotated at some pivot. |
| 21 | + -104 <= target <= 104 |
| 22 | +
|
| 23 | +*/ |
| 24 | + |
| 25 | +/* |
| 26 | +Time Complexity: O(log(N)), where N is the size of the array |
| 27 | +Space Complexity: O(1), Constant Space |
| 28 | +*/ |
| 29 | + |
| 30 | +#include <bits/stdc++.h> |
| 31 | +using namespace std; |
| 32 | + |
| 33 | +int binarySearch(vector<int> arr, int l, int r, int x) // Function to perform Binary Search |
| 34 | +{ |
| 35 | + while(l<=r) |
| 36 | + { |
| 37 | + int m=(l+r)/2; |
| 38 | + if(arr[m]==x) |
| 39 | + return m; |
| 40 | + else if(arr[m]<x) |
| 41 | + l=m+1; |
| 42 | + else |
| 43 | + r=m-1; |
| 44 | + } |
| 45 | + return -1; |
| 46 | +} |
| 47 | +int search(const vector<int> &A, int B) |
| 48 | +{ |
| 49 | + int l=0; // lower bound |
| 50 | + int n=A.size(); |
| 51 | + int u=n-1; // upper bound |
| 52 | + int index=-1; // to know the index upto which array is sorted i.e left,right of this will be sorted array |
| 53 | + while(l<=u) |
| 54 | + { |
| 55 | + int m=(l+u)/2; |
| 56 | + int prev=(m-1+n)%n; |
| 57 | + int next=(m+1)%n; |
| 58 | + if(A[l]<=A[u]) // If array is already sorted return lower bound |
| 59 | + { |
| 60 | + index=l; |
| 61 | + break; |
| 62 | + } |
| 63 | + if(A[m]<=A[prev]&&A[m]<=A[next]) // if value at middle is less than or equal to value at previous and value at middle is also less than or equal to value at next => we have found our match |
| 64 | + { |
| 65 | + index=m; |
| 66 | + break; |
| 67 | + } |
| 68 | + else if(A[l]<=A[m]) // if value at lower bound is less than value at middle then shift lower bound |
| 69 | + l=next; |
| 70 | + else// if above condition is not true shift upper bound |
| 71 | + u=prev; |
| 72 | + } |
| 73 | + int i1=binarySearch(A,0,index-1,B); // performing binary search in either halves of the sorted array |
| 74 | + if(i1!=-1) |
| 75 | + return i1; |
| 76 | + i1=binarySearch(A,index,n-1,B); |
| 77 | + return i1; |
| 78 | +} |
| 79 | + |
| 80 | +int main() // driver function |
| 81 | +{ |
| 82 | + vector<int> array = {4,5,6,7,0,1,2}; |
| 83 | + int ans = search(array,0); |
| 84 | + cout << ans; |
| 85 | + return 0; |
| 86 | +} |
0 commit comments