Find the Minimum element in a Sorted and Rotated Array
Given a sorted array arr[] (may be distinct or may contain duplicates) of size N that is rotated at some unknown point, the task is to find the minimum element in it.
Examples:
Input: arr[] = {5, 6, 1, 2, 3, 4}
Output: 1
Explanation: 1 is the minimum element present in the array.Input: arr[] = {1, 2, 3, 4}
Output: 1Input: arr[] = {2, 1}
Output: 1
Find the minimum element in a sorted and rotated array using Linear Search:
A simple solution is to use linear search to traverse the complete array and find a minimum.
Follow the steps mentioned below to implement the idea:
- Declare a variable (say min_ele) to store the minimum value and initialize it with arr[0].
- Traverse the array from the start.
- Update the minimum value (min_ele) if the current element is less than it.
- Return the final value of min_ele as the required answer.
Below is the implementation of the above approach.
C++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Function to find the minimum value int findMin( int arr[], int n) { int min_ele = arr[0]; // Traversing over array to // find minimum element for ( int i = 0; i < n; i++) { if (arr[i] < min_ele) { min_ele = arr[i]; } } return min_ele; } // Driver code int main() { int arr[] = { 5, 6, 1, 2, 3, 4 }; int N = sizeof (arr) / sizeof (arr[0]); // Function call cout << findMin(arr, N) << endl; return 0; } |
Java
/*package whatever //do not write package name here */ import java.io.*; class GFG { // Function to find the minimum value static int findMin( int arr[], int n) { int min_ele = arr[ 0 ]; // Traversing over array to // find minimum element for ( int i = 0 ; i < n; i++) { if (arr[i] < min_ele) { min_ele = arr[i]; } } return min_ele; } public static void main (String[] args) { int arr[] = { 5 , 6 , 1 , 2 , 3 , 4 }; int N = arr.length; System.out.println(findMin(arr, N)); } } // This code is contributed by aadityaburujwale. |
Python3
# python3 code to implement the approach def findMin(arr, N): min_ele = arr[ 0 ]; # Traversing over array to # find minimum element for i in range (N) : if arr[i] < min_ele : min_ele = arr[i] return min_ele; # Driver program arr = [ 5 , 6 , 1 , 2 , 3 , 4 ] N = len (arr) print (findMin(arr,N)) # This code is contributed by aditya942003patil |
C#
// C# code to implement above approach using System; class Minimum { static int findMin( int [] arr, int N) { int min_ele = arr[0]; // Traversing over array to // find minimum element for ( int i = 0; i < N; i++) { if (arr[i] < min_ele) { min_ele = arr[i]; } } return min_ele; } // Driver Program public static void Main() { int [] arr = { 5, 6, 1, 2, 3, 4 }; int N = arr.Length; Console.WriteLine(findMin(arr, N)); } } // This code is contributed by aditya942003patil. |
Javascript
// JS code to implement the approach // Function to find the minimum value function findMin(arr, n) { let min_ele = arr[0]; // Traversing over array to // find minimum element for (let i = 0; i < n; i++) { if (arr[i] < min_ele) { min_ele = arr[i]; } } return min_ele; } // Driver code let arr = [5, 6, 1, 2, 3, 4]; let N = arr.length; // Function call console.log(findMin(arr, N)); // This code is contributed by adityamaharshi21. |
1
Time Complexity: O(N)
Auxiliary Space: O(1)
The Easy Way using STL:
The Approach:
The is verry simple here we have array/vector we use *min_element function in stl and print the minimum element in the vector/array.
C++
#include <iostream> #include<bits/stdc++.h> using namespace std; int main() { vector< int >v{5, 6, 1, 2, 3, 4}; cout<< "The Minimum Element in the vector is: " ; cout<<*min_element(v.begin(),v.end())<<endl; return 0; } |
Java
/*package whatever //do not write package name here */ import java.util.*; class GFG { public static void main(String[] args) { List<Integer> v = new ArrayList<>( Arrays.asList( 5 , 6 , 1 , 2 , 3 , 4 )); System.out.print( "The Minimum Element in the vector is: " ); System.out.println(Collections.min(v)); } } // contributed by akashish__ |
Python3
from typing import List def find_min(arr: List [ int ]) - > int : return min (arr) v = [ 5 , 6 , 1 , 2 , 3 , 4 ] print ( "The Minimum Element in the vector is: " , find_min(v)) |
C#
// C# program to find minimum elementin array using System; using System.Linq; class GFG { static int find_min( int []arr) { return arr.Min(); } // Driver code public static void Main(String[] args) { int []arr = {5, 6, 1, 2, 3, 4}; Console.WriteLine( "The Minimum Element in the vector is: " + find_min(arr)); } } // This code is contributed by Pratik Gupta (guptapratik) |
Javascript
function findMin(arr) { return Math.min(...arr); } let v = [5, 6, 1, 2, 3, 4]; console.log( "The Minimum Element in the vector is: " , findMin(v)); |
The Minimum Element in the vector is: 1
Time Complexity: O(N),in the worst case.
Auxiliary Space: O(1)
Find the minimum element in a sorted and rotated array using Binary Search:
This approach is based on the following idea:
As the array is sorted and rotated, there are two segments that are themselves sorted but their meeting point is the only position where the smallest element is and that is not sorted.
So we just need to find the position whose neighbours are greater than it and based on the extreme end values we can decide in which half we should search for that element.
Follow the steps below to solve the given problem:
If we take a closer look at the above examples, we can easily figure out the following pattern:
- The minimum element is the only element whose previous is greater than it. If there is no previous element, then there is no rotation (the first element is minimum).
- We check this condition for the middle element by comparing it with (mid-1)th and (mid+1)th elements.
- If the minimum element is not at the middle (neither mid nor mid + 1), then:
- If the middle element is smaller than the last element, then the minimum element lies in the left half
- Else minimum element lies in the right half.
Follow the below illustration for a better understanding
Illustration:
Let the array be arr[]={15, 18, 2, 3, 6, 12}
low = 0 , high = 5.
=> mid = 2
=> arr[mid]=2 , arr[mid-1] > arr[mid] , hence condition is matched
=> The required index = mid = 2So the element is found at index 2 and arr[2] = 2
Below is the code implementation of the above approach:
C++
// C++ program to find minimum // element in a sorted and rotated array #include <bits/stdc++.h> using namespace std; int findMin( int arr[], int low, int high) { // This condition is needed to // handle the case when array is not // rotated at all if (high < low) return arr[0]; // If there is only one element left if (high == low) return arr[low]; // Find mid int mid = low + (high - low) / 2; /*(low + high)/2;*/ // Check if element (mid+1) is minimum element. Consider // the cases like {3, 4, 5, 1, 2} if (mid < high && arr[mid + 1] < arr[mid]) return arr[mid + 1]; // Check if mid itself is minimum element if (mid > low && arr[mid] < arr[mid - 1]) return arr[mid]; // Decide whether we need to go to left half or right // half if (arr[high] > arr[mid]) return findMin(arr, low, mid - 1); return findMin(arr, mid + 1, high); } // Driver program to test above functions int main() { int arr[] = { 5, 6, 1, 2, 3, 4 }; int N = sizeof (arr) / sizeof (arr[0]); // Function call cout << "The minimum element is " << findMin(arr, 0, N - 1) << endl; return 0; } // This is code is contributed by rathbhupendra |
C
// C program to find minimum element // in a sorted and rotated array #include <stdio.h> int findMin( int arr[], int low, int high) { // This condition is needed to handle the case when // array is not rotated at all if (high < low) return arr[0]; // If there is only one element left if (high == low) return arr[low]; // Find mid. (low + high)/2 int mid = low + (high - low) / 2; // Check if element (mid+1) is minimum element. if (mid < high && arr[mid + 1] < arr[mid]) return arr[mid + 1]; // Check if mid itself is minimum element if (mid > low && arr[mid] < arr[mid - 1]) return arr[mid]; // Decide whether we need to go to // left half or right half if (arr[high] > arr[mid]) return findMin(arr, low, mid - 1); return findMin(arr, mid + 1, high); } // Driver program to test above functions int main() { int arr[] = { 5, 6, 1, 2, 3, 4 }; int N = sizeof (arr) / sizeof (arr[0]); // Function call printf ( "The minimum element is %d\n" , findMin(arr, 0, N - 1)); return 0; } |
Java
// Java program to find minimum element in a sorted and // rotated array import java.io.*; import java.lang.*; import java.util.*; class Minimum { static int findMin( int arr[], int low, int high) { // This condition is needed to handle the case when // array is not rotated at all if (high < low) return arr[ 0 ]; // If there is only one element left if (high == low) return arr[low]; // Find mid int mid = low + (high - low) / 2 ; /*(low + high)/2;*/ // Check if element (mid+1) is minimum element. // Consider the cases like {3, 4, 5, 1, 2} if (mid < high && arr[mid + 1 ] < arr[mid]) return arr[mid + 1 ]; // Check if mid itself is minimum element if (mid > low && arr[mid] < arr[mid - 1 ]) return arr[mid]; // Decide whether we need to go to left half or // right half if (arr[high] > arr[mid]) return findMin(arr, low, mid - 1 ); return findMin(arr, mid + 1 , high); } // Driver Program public static void main(String[] args) { int arr[] = { 5 , 6 , 1 , 2 , 3 , 4 }; int N = arr.length; System.out.println( "The minimum element is " + findMin(arr, 0 , N - 1 )); } } |
Python3
# Python program to find minimum element # in a sorted and rotated array def findMin(arr, low, high): # This condition is needed to handle the case when array is not # rotated at all if high < low: return arr[ 0 ] # If there is only one element left if high = = low: return arr[low] # Find mid mid = int ((low + high) / 2 ) # Check if element (mid+1) is minimum element. Consider # the cases like [3, 4, 5, 1, 2] if mid < high and arr[mid + 1 ] < arr[mid]: return arr[mid + 1 ] # Check if mid itself is minimum element if mid > low and arr[mid] < arr[mid - 1 ]: return arr[mid] # Decide whether we need to go to left half or right half if arr[high] > arr[mid]: return findMin(arr, low, mid - 1 ) return findMin(arr, mid + 1 , high) # Driver program to test above functions if __name__ = = '__main__' : arr = [ 5 , 6 , 1 , 2 , 3 , 4 ] N = len (arr) print ( "The minimum element is " + \ str (findMin(arr, 0 , N - 1 ))) # This code is contributed by Pratik Chhajer |
C#
// C# program to find minimum element // in a sorted and rotated array using System; class Minimum { static int findMin( int [] arr, int low, int high) { // This condition is needed to handle // the case when array // is not rotated at all if (high < low) return arr[0]; // If there is only one element left if (high == low) return arr[low]; // Find mid // (low + high)/2 int mid = low + (high - low) / 2; // Check if element (mid+1) is minimum element. // Consider the cases like {3, 4, 5, 1, 2} if (mid < high && arr[mid + 1] < arr[mid]) return arr[mid + 1]; // Check if mid itself is minimum element if (mid > low && arr[mid] < arr[mid - 1]) return arr[mid]; // Decide whether we need to go to // left half or right half if (arr[high] > arr[mid]) return findMin(arr, low, mid - 1); return findMin(arr, mid + 1, high); } // Driver Program public static void Main() { int [] arr = { 5, 6, 1, 2, 3, 4 }; int N = arr.Length; Console.WriteLine( "The minimum element is " + findMin(arr, 0, N - 1)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to find minimum // element in a sorted and // rotated array function findMin( $arr , $low , $high ) { // This condition is needed // to handle the case when // array is not rotated at all if ( $high < $low ) return $arr [0]; // If there is only // one element left if ( $high == $low ) return $arr [ $low ]; // Find mid $mid = $low + ( $high - $low ) / 2; /*($low + $high)/2;*/ // Check if element (mid+1) // is minimum element. // Consider the cases like // (3, 4, 5, 1, 2) if ( $mid < $high && $arr [ $mid + 1] < $arr [ $mid ]) return $arr [ $mid + 1]; // Check if mid itself // is minimum element if ( $mid > $low && $arr [ $mid ] < $arr [ $mid - 1]) return $arr [ $mid ]; // Decide whether we need // to go to left half or // right half if ( $arr [ $high ] > $arr [ $mid ]) return findMin( $arr , $low , $mid - 1); return findMin( $arr , $mid + 1, $high ); } // Driver Code $arr = array (5, 6, 1, 2, 3, 4); $N = sizeof( $arr ); echo "The minimum element is " . findMin( $arr , 0, $N - 1) . "\n" ; // This code is contributed by ChitraNayal ?> |
Javascript
<script> // Javascript program to find minimum element in a sorted and rotated array function findMin(arr,low,high) { // This condition is needed to handle the case when array // is not rotated at all if (high < low) return arr[0]; // If there is only one element left if (high == low) return arr[low]; // Find mid let mid =low + Math.floor((high - low)/2); /*(low + high)/2;*/ // Check if element (mid+1) is minimum element. Consider // the cases like {3, 4, 5, 1, 2} if (mid < high && arr[mid+1] < arr[mid]) return arr[mid+1]; // Check if mid itself is minimum element if (mid > low && arr[mid] < arr[mid - 1]) return arr[mid]; // Decide whether we need to go to left half or right half if (arr[high] > arr[mid]) return findMin(arr, low, mid-1); return findMin(arr, mid+1, high); } // Driver Program let arr=[5, 6, 1, 2, 3, 4]; let N = arr.length; document.write( "The minimum element is " + findMin(arr, 0, N-1)+ "<br>" ); // This code is contributed by avanitrachhadiya2155 </script> |
The minimum element is 1
Time Complexity: O(logN), using binary search
Auxiliary Space: O(1)
Find the minimum element in a sorted and rotated array using Modified Binary Search:
- The findMin function takes three arguments: arr (the input array), low (the lowest index of the array), and high (the highest index of the array).
- If the array is not rotated, i.e., the first element is less than or equal to the last element, then the first element is returned as the minimum element.
- Otherwise, a binary search algorithm is implemented to find the minimum element.
- The binary search loop continues until the low index is less than or equal to the high index.
- Inside the loop, the middle index mid is calculated as the average of the low and high indices.
- If the element at the mid index is less than the element at mid-1 index, then the element at the mid index is returned as the minimum element.
- If the element at the mid index is greater than the element at the high index, then the minimum element must be in the left half of the array. So, the low index is updated to mid+1.
- Otherwise, the minimum element must be in the right half of the array. So, the high index is updated to mid-1.
- If no minimum element is found during the binary search, then the findMin function returns None.
- In the driver program, an input array arr is defined with rotated and sorted elements, and the findMin function is called with low and high indices set to 0 and N-1, respectively, where N is the length of the input array.
- The minimum element is printed using the print function with the help of the str function to convert the returned value to a string
Illustration:
Let the array be arr = [7, 8, 9, 1, 2, 3, 4, 5, 6]. Here, the minimum element in the array is 1.
We start with low = 0 and high = 8.
First iteration:
mid = (low + high) // 2 = 4
arr[mid] = 2, arr[mid-1] = 1
arr[mid] < arr[mid-1], so we return arr[mid] which is 2.
So the minimum element is found at index 3 and arr[3] = 1.
Below is the code implementation of the above approach:
C++
#include <bits/stdc++.h> using namespace std; int findMin(vector< int >& arr, int low, int high) { // If the array is not rotated if (arr[low] <= arr[high]) { return arr[low]; } // Binary search while (low <= high) { int mid = (low + high) / 2; // Check if mid is the minimum element if (arr[mid] < arr[mid - 1]) { return arr[mid]; } // If the right half is sorted, the minimum element // must be in the left half if (arr[mid] > arr[high]) { low = mid + 1; } // If the left half is sorted, the minimum element // must be in the right half else { high = mid - 1; } } // If no minimum element is found, return -1 return -1; } // Driver program to test above functions int main() { vector< int > arr = { 5, 6, 1, 2, 3, 4 }; int N = arr.size(); cout << "The minimum element is " << findMin(arr, 0, N - 1) << endl; return 0; } |
Python3
def findMin(arr, low, high): # If the array is not rotated if arr[low] < = arr[high]: return arr[low] # Binary search while low < = high: mid = (low + high) / / 2 # Check if mid is the minimum element if arr[mid] < arr[mid - 1 ]: return arr[mid] # If the right half is sorted, the minimum element must be in the left half if arr[mid] > arr[high]: low = mid + 1 # If the left half is sorted, the minimum element must be in the right half else : high = mid - 1 # If no minimum element is found, return None return None # Driver program to test above functions if __name__ = = '__main__' : arr = [ 5 , 6 , 1 , 2 , 3 , 4 ] N = len (arr) print ( "The minimum element is " + \ str (findMin(arr, 0 , N - 1 ))) |
C#
using System; using System.Collections.Generic; public class Program { public static int findMin(List< int > arr, int low, int high) { // If the array is not rotated if (arr[low] <= arr[high]) { return arr[low]; } // Binary search while (low <= high) { int mid = (low + high) / 2; // Check if mid is the minimum element if (arr[mid] < arr[mid - 1]) { return arr[mid]; } // If the right half is sorted, the minimum element must be in the left half if (arr[mid] > arr[high]) { low = mid + 1; } // If the left half is sorted, the minimum element must be in the right half else { high = mid - 1; } } // If no minimum element is found, return -1 return -1; } // Driver program to test above functions public static void Main() { List< int > arr = new List< int > {5, 6, 1, 2, 3, 4}; int N = arr.Count; Console.WriteLine( "The minimum element is " + findMin(arr, 0, N - 1)); } } // This code is contributed by Prajwal Kandekar |
Java
import java.util.*; public class Main { public static int findMin(List<Integer> arr, int low, int high) { // If the array is not rotated if (arr.get(low) <= arr.get(high)) { return arr.get(low); } // Binary search while (low <= high) { int mid = (low + high) / 2 ; // Check if mid is the minimum element if (arr.get(mid) < arr.get(mid - 1 )) { return arr.get(mid); } // If the right half is sorted, the minimum // element must be in the left half if (arr.get(mid) > arr.get(high)) { low = mid + 1 ; } // If the left half is sorted, the minimum // element must be in the right half else { high = mid - 1 ; } } // If no minimum element is found, return -1 return - 1 ; } // Driver program to test above functions public static void main(String[] args) { List<Integer> arr = new ArrayList<>( Arrays.asList( 5 , 6 , 1 , 2 , 3 , 4 )); int N = arr.size(); System.out.println( "The minimum element is " + findMin(arr, 0 , N - 1 )); } } |
Javascript
function findMin(arr, low, high) { // If the array is not rotated if (arr[low] <= arr[high]) { return arr[low]; } // Binary search while (low <= high) { let mid = Math.floor((low + high) / 2); // Check if mid is the minimum element if (arr[mid] < arr[mid - 1]) { return arr[mid]; } // If the right half is sorted, the minimum element must be in the left half if (arr[mid] > arr[high]) { low = mid + 1; } // If the left half is sorted, the minimum element must be in the right half else { high = mid - 1; } } // If no minimum element is found, return -1 return -1; } // Driver program to test above functions let arr = [5, 6, 1, 2, 3, 4]; let N = arr.length; console.log( "The minimum element is " + findMin(arr, 0, N - 1)); |
The minimum element is 1
Time complexity: O(log n) – where n is the number of elements in the array. This is because the algorithm uses binary search, which has a logarithmic time complexity.
Auxiliary Space: O(1) – the algorithm uses a constant amount of extra space to store variables such as low, high, and mid, regardless of the size of the input array.
Find the minimum element in a sorted and rotated array using Hashing:
- Initialize the input array.
- Find the size of the input array.
- Declare an unordered set to keep track of unique elements encountered so far, and initialize the minimum element as the first element of the array.
- Iterate over each element of the array.
- Check if the current element is already present in the set using the find method.
- If the element is not present, insert it into the set.
- Check if the current element is less than the current minimum element.
- If it is, update the minimum element to be the current element.
- After iterating over all elements of the array, output the minimum element.
C++
#include <iostream> #include <unordered_set> using namespace std; int main() { int arr[] = { 5, 6, 1, 2, 3, 4 }; int n = sizeof (arr) / sizeof (arr[0]); unordered_set< int > s; int min_element = arr[0]; for ( int i = 0; i < n; i++) { if (s.find(arr[i]) == s.end()) { s.insert(arr[i]); if (arr[i] < min_element) { min_element = arr[i]; } } } cout << "Minimum element in the array is: " << min_element << endl; return 0; } |
Minimum element in the array is: 1
Time Complexity : O(N)
Auxiliary Space : O(N)
Please Login to comment...