Skip to content

Commit 7b4aea5

Browse files
Merge pull request #165 from FazeelUsmani/findMin
Create 23_findMinInRotatedSorted.cpp
2 parents 31f04e5 + 4b02012 commit 7b4aea5

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
int findMin(vector<int>& nums) {
2+
3+
int l = 0, r = nums.size()-1;
4+
5+
if(nums[l]<nums[r]) // already sorted (0 rotation)
6+
return nums[0]; // return first element
7+
8+
while(l<r)
9+
{
10+
int mid = l + (r-l)/2; // finding middle
11+
12+
if(nums[mid]>nums[r]){
13+
l = mid + 1;
14+
}
15+
else if(nums[l]>nums[mid]){
16+
r = mid;
17+
}
18+
else{
19+
r--; //overcome duplicate elements
20+
}
21+
}
22+
return nums[r];
23+
}

0 commit comments

Comments
 (0)