Advertisement
vaibhav1906

Minimum is rotated sorted array

Nov 25th, 2021
1,231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int findMin(vector<int>& nums) {
  4.        
  5.         int n = nums.size();
  6.        
  7.         int l = 0, h = n-1;
  8.        
  9.         while(l<h){
  10.            
  11.             int m = l + (h-l)/2;
  12.            
  13.             if(nums[0]-nums[m]>0){
  14.                 h = m;
  15.             }
  16.             else{
  17.                 l = m+1;
  18.             }
  19.  
  20.            
  21.         }
  22.        
  23.         if(nums[0]-nums[l]<0) return nums[0]; //If the array is completely sorted
  24.        
  25.         return nums[l];
  26.        
  27.     }
  28. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement