Skip to content

Commit

Permalink
Create Search_in_Rotated_Sorted_Array.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
khushisaxena17 authored Oct 1, 2023
1 parent ffe1e15 commit 76728b9
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions LeetCodeQuestions/Search_in_Rotated_Sorted_Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
public:
int search(vector<int>& nums, int target) {
//find the Rotation point!
int low=0,high=nums.size()-1;
while(low<=high){
int mid=(low+high)/2;
if(nums[mid]==target){
return mid;
}
else if(nums[mid]>=nums[low]){//sorted left half
if(nums[mid]>target && nums[low]<=target){
high=mid-1;
}
else{
low=mid+1;
}
}
else if(nums[mid]<=nums[high]){//sorted right half
if(nums[mid]<target && nums[high]>=target){
low=mid+1;
}
else{
high=mid-1;
}
}
}
return -1;
}
};

0 comments on commit 76728b9

Please sign in to comment.