Minimum Number in a sorted rotated array | Practice | GeeksforGeeks

Given an array of distinct elements which was initially sorted. This array is rotated at some unknown point. The task is to find the minimum element in the given sorted and rotated array. 
Example 1:
Input:
N = 10
arr[] = {2,3,4,5,6,7,8,9,10,1}
Output: 1
Explanation: The array is rotated 
once anti-clockwise. So minium 
element is at last index (n-1) 
which is 1.
Example 2:
Input:
N = 5
arr[] = {3,4,5,1,2}
Output: 1
Explanation: The array is rotated 
and the minimum element present is
at index (n-2) which is 1.

Your Task:
The task is to complete the function minNumber() which takes the array arr[] and its starting and ending indices (low and high) as inputs and returns the minimum element in the given sorted and rotated array.
Expected Time Complexity: O(LogN).
Expected Auxiliary Space: O(LogN).
Constraints:
1 <= N <= 107
1 <= arr[i] <= 107
0
pkalyanramcec191 day ago
int min = *min_element(arr,arr+high-low+1);
0
harshitsinghparmar902422 days ago
def minNumber(self, arr,low,high):       return min(arr)
 
0
vikasrajpoot4792 days ago
//using binary search
int minNumber(int arr[], int low, int high){   int ans=-1;  while(low<=high)  {      int mid=low+(high-low)/2;      if(arr[0]<=arr[mid])      {          low=mid+1;      }      else{          ans=arr[mid];          high=mid-1;      }        }  if(ans==-1)  {      return arr[0];  }  return ans;   }
0
swapnilsrkr6 days ago
//Time complexity -> O(LogN)
//Space complexity -> O(1)

int minNumber(int arr[], int low, int high)
    {
        if(high + 1 == 1)
            return arr[0];
        
        if(high + 1 == 2)
            return arr[0] < arr[1] ? arr[0] : arr[1];
            
        int mid = low + (high - low)/2, minElement = -1;
        
        while(low <= high){
            if(arr[0] > arr[mid]){
                minElement = arr[mid];
                high = mid - 1;
            }
            else
                low = mid + 1;
            mid = low + (high - low)/2;
        }
        
        //Below if condition will be true, only when all elements are in ascending order
        if(minElement == -1)
            return arr[0];
        
        return minElement; 
    }
0
tantade20021 week ago
//using two pointer approach
 
 static int minNumber(int arr[], int low, int high)   {          // Your code here           int left =1;           int right=arr.length-2;           while(left<=right){               if(arr[left]<arr[left-1]&&arr[left]<arr[left+1]){                   return arr[left];               }               else if(arr[right]<arr[right-1]&& arr[right]<arr[right+1]){                   return arr[right];               }               else{                   left++;                   right--;               }           }           if(arr[0]>arr[arr.length-1]){               return arr[arr.length-1];           }           return arr[0];               }
0
jituverma70491 week ago
  //simple solution to above problem
//Function to find the minimum element in sorted and rotated array.   int minnumber(int arr[], int low, int high)   {       sort(arr,arr+high+1);     return arr[0];          }
+2
dhruv08092 weeks ago
int minNumber(int arr[], int low, int high)   {
       // Time complexity O(logn)
       // Space complexity O(1)       // Your code here       int res = low;       while(high>=low){           int mid = (high+low)/2;           if(arr[mid]>arr[res]) low = mid+1;           else {               res = mid;               high = mid-1;           }       }       return arr[res];   }
0
dhruv0809
This comment was deleted.
+1
anant41363 weeks ago
int minNumber(int arr[], int low, int high)   {       // Your code here       while(low<high)       {       int m=(low+high)/2;       if(arr[m]<arr[m+1]&&arr[m]<arr[high])high=m;       else if(arr[m]<arr[m+1]){return(min(minNumber(arr,low,m),minNumber(arr,m+1,high)));}              else if(arr[m]>arr[m+1])low=m+1;              }       return arr[low];   }
0
shubham211019973 weeks ago
static int minNumber(int arr[], int low, int high)   {       low=0;       high=arr.length-1;        if(arr[low]<arr[high]) return arr[low];//not rotated       while(low<=high){           int mid=(low+high)/2;           if(arr[mid]<arr[mid-1])return arr[mid];           if(arr[mid]>arr[mid+1])return arr[mid+1];           else if(arr[mid]>arr[low])             low=mid+1;            else if(arr[mid]<arr[high])            high=mid-1;       }       return -1;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.