Minimum element in a sorted and rotated array | Practice | GeeksforGeeks

A sorted array A[ ] with distinct elements is rotated at some unknown point, the task is to find the minimum element in it.
Example 1
Input:
N = 5
arr[] = {4 ,5 ,1 ,2 ,3}
Output: 1
Explanation: 1 is the minimum element inthe array.
Example 2
Input:
N = 7
arr[] = {10, 20, 30, 40, 50, 5, 7}
Output: 5
Explanation: Here 5 is the minimum element.
 
Your Task:
Complete the function findMin() which takes an array arr[] and n, size of the array as input parameters, and returns the minimum element of the array.
Expected Time Complexity: O(log N).
Expected Auxiliary Space: O(log N).
Constraints:
1 ≤ N ≤ 100000
1 ≤ A[i] ≤ 1000000
0
edavellycharanya7 hours ago
C++
int findMin(int arr[], int n){
        sort(arr,arr+n);
        return arr[0];
    }
0
edavellycharanya
This comment was deleted.
0
chessnoobdj8 hours ago
C++
int findMin(int arr[], int n){
        int l = 0, r = n-1, mid;
        while(l < r){
            mid = (l+r)/2;
            if(arr[mid] < arr[r])
                r = mid;
            else
                l = mid+1;
        }
        return arr[l];
    }
0
chessnoobdj8 hours ago
C++ log(n)
int findMin(int arr[], int n){
        int l = 0, r = n-1, mid, ans = arr[0];
        while(l < r){
            mid = l + (r-l)/2;
            if(arr[0] < arr[mid])
                l = mid+1;
            else
                r = mid;
        }
        ans = min(ans, arr[mid]);
        if(mid-1 >= 0)
            ans = min(ans, arr[mid-1]);
        if(mid+1 < n)
            ans = min(ans, arr[mid+1]);
        return ans;
    }
0
awrijeetsingh12025 days ago
//USING ARRAY REPRESENTATION OF A BINARY TREE

int findMin(int arr[], int n)
{
	int i, min = 263526;
	for (i = 0; i < n / 2; i++)
	{
		if (arr[i] < min)
		{
			min = arr[i];
		}
		if (2 * i + 1 < n && arr[2 * i + 1] < min)
		{
			min = arr[2 * i + 1];
		}
		if (2 * i + 2 < n && arr[2 * i + 2] < min)
		{
			min = arr[2 * i + 2];
		}
	}
	return min;
}
0
meghapucsd5 days ago
int findMin(int arr[], int n){       //complete the function here       int i = 0;        int j =n-1;       int min = arr[0];       while (i<j-1 && j>0)       {                   if (arr[i] < arr[i +1])           {               i++;           }                      if (arr[j] > arr[j-1])           {               j--;           }                                     if (arr[i] <= arr[j] && min>arr[i])           {               min = arr[i];           }           else if (min>arr[j])           {             min = arr[j];           }                             }             return min;          }
0
harshilrpanchal19986 days ago
java solution
 
class Solution{   int findMin(int arr[], int n)   {       //complete the function here        int ans = arr[0];       for (int i=1;i < n ; i++){           if (arr[i] < ans){               ans = arr[i];           }       }       return ans;   }} 
0
amarrajsmart1972 weeks ago
int findMin(int arr[], int n){       //complete the function here       // int min=INT_MAX;       // for(int i=0;i<n;i++)       // {       //     if(arr[i]<min)       //     min=arr[i];       // }       // return min;       for(int i=0;i<n-1;i++){       if(arr[i]>arr[i+1])       {           return arr[i+1];       }       }       return arr[0];   }
+1
sarthakgupta102 weeks ago
int findMin(int arr[], int n){
        //complete the function here
        int min=INT_MAX;
        for(int i=0;i<n;i++)
        {
            if(arr[i]<min)
            {
                min=arr[i];
            }
        }
        return min;
        
    }
0
hemachoudary1232 weeks ago
class Solution{   int findMin(int arr[], int n)   {       Arrays.sort(arr);       return arr[0];   }} 
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.