From 152a125a3140991348aa92a89031f576add23b0a Mon Sep 17 00:00:00 2001 From: fulpopy <82092014+fulpopy@users.noreply.github.com> Date: Mon, 17 Oct 2022 16:59:45 +0530 Subject: [PATCH] Search in Rotated Sorted Array --- C++/SearchinRotatedSortedArray.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 C++/SearchinRotatedSortedArray.cpp diff --git a/C++/SearchinRotatedSortedArray.cpp b/C++/SearchinRotatedSortedArray.cpp new file mode 100644 index 0000000..647b462 --- /dev/null +++ b/C++/SearchinRotatedSortedArray.cpp @@ -0,0 +1,25 @@ +int search(vector& nums, int target) { + int n = nums.size(); + int lo = 0; + int hi = n-1; + while(lonums[hi]) lo = mid+1; + else hi = mid; + } + int res = bs(nums,0,n-1,target,lo,n); + return res; + +} +int bs(vector & arr,int start,int end,int target,int lo,int n){ + while(start<=end){ + // start = (start+lo)%n; + // end = (end+start)%n; + int mid = start+((end-start)/2); + int realmid = (mid+lo)%n; + if(arr[realmid]==target) return realmid; + else if(arr[realmid]>target) end = mid-1; + else start = mid+1; + } + return -1; +}