Given a sorted array of distinct elements arr[] 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[] = [3, 1, 2]
Output: 1
Explanation: 1 is the minimum element present in the array.Input: arr[] = [4, 2, 3]
Output: 2
Explanation: 2 is the only minimum element in the array.
Table of Content
[Naive Approach] Linear Search - O(n) Time and O(1) Space
A simple solution is to use linear search to traverse the complete array and find a minimum.
#include <iostream>
#include <vector>
using namespace std;
int findMin(vector<int>& arr) {
int res = arr[0];
// Traverse over arr[] to find minimum element
for (int i = 1; i < arr.size(); i++)
res = min(res, arr[i]);
return res;
}
int main() {
vector<int> arr = {5, 6, 1, 2, 3, 4};
int n = arr.size();
cout << findMin(arr) << endl;
return 0;
}
#include <stdio.h>
int findMin(int arr[], int n) {
int res = arr[0];
// Traverse over arr[] to find minimum element
for (int i = 1; i < n; i++) {
if (arr[i] < res) {
res = arr[i];
}
}
return res;
}
int main() {
int arr[] = {5, 6, 1, 2, 3, 4};
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d\n", findMin(arr, n));
return 0;
}
import java.util.*;
class GfG {
static int findMin(int[] arr) {
int res = arr[0];
// Traverse over arr[] to find minimum element
for (int i = 1; i < arr.length; i++) {
res = Math.min(res, arr[i]);
}
return res;
}
public static void main(String[] args) {
int[] arr = {5, 6, 1, 2, 3, 4};
System.out.println(findMin(arr));
}
}
def findMin(arr):
res = arr[0]
# Traverse over arr[] to find minimum element
for i in range(1, len(arr)):
res = min(res, arr[i])
return res
if __name__ == "__main__":
arr = [5, 6, 1, 2, 3, 4]
print(findMin(arr))
using System;
using System.Collections.Generic;
class GfG {
static int findMin(int[] arr) {
int res = arr[0];
// Traverse over arr[] to find minimum element
for (int i = 1; i < arr.Length; i++) {
res = Math.Min(res, arr[i]);
}
return res;
}
static void Main() {
int[] arr = { 5, 6, 1, 2, 3, 4 };
Console.WriteLine(findMin(arr));
}
}
function findMin(arr) {
let res = arr[0];
// Traverse over arr[] to find minimum element
for (let i = 1; i < arr.length; i++) {
res = Math.min(res, arr[i]);
}
return res;
}
// Driver code
const arr = [5, 6, 1, 2, 3, 4];
console.log(findMin(arr));
Output
1
[Expected Approach] Binary Search - O(log n) Time and O(1) Space
The array is sorted and then rotated, so it consists of two sorted parts, with the minimum element located at the rotation point.
Using Binary Search, we can efficiently narrow down the part that contains the minimum by checking whether the current range is already sorted and by comparing the middle element with the last element.
Steps for Implementation:
- If
arr[low] < arr[high], the current range is already sorted, returnarr[low]. - Compute
mid. - If
arr[mid] > arr[high], the minimum lies to the right ofmid,setlow = mid + 1. - Otherwise, the minimum lies at
midor to its left, sethigh = mid. - When
low == high, that index stores the minimum element.
#include <iostream>
#include <vector>
using namespace std;
int findMin(vector<int> &arr) {
int low = 0, high = arr.size() - 1;
while (low < high) {
// If current range is sorted, first element is minimum
if (arr[low] < arr[high])
return arr[low];
int mid = low + (high - low) / 2;
// Minimum lies in right half
if (arr[mid] > arr[high])
low = mid + 1;
// Minimum lies in left half (including mid)
else
high = mid;
}
// low == high points to the minimum element
return arr[low];
}
int main() {
vector<int> arr = {5, 6, 1, 2, 3, 4};
cout << findMin(arr);
return 0;
}
#include <stdio.h>
int findMin(int arr[], int n) {
int low = 0, high = n - 1;
while (low < high) {
// If current range is sorted, first element is minimum
if (arr[low] < arr[high])
return arr[low];
int mid = low + (high - low) / 2;
// Minimum lies in right half
if (arr[mid] > arr[high])
low = mid + 1;
// Minimum lies in left half (including mid)
else
high = mid;
}
// low == high points to the minimum element
return arr[low];
}
int main() {
int arr[] = {5, 6, 1, 2, 3, 4};
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d", findMin(arr, n));
return 0;
}
import java.util.*;
class GfG {
static int findMin(int[] arr) {
int low = 0, high = arr.length - 1;
while (low < high) {
// If current range is sorted, first element is minimum
if (arr[low] < arr[high])
return arr[low];
int mid = low + (high - low) / 2;
// Minimum lies in right half
if (arr[mid] > arr[high])
low = mid + 1;
// Minimum lies in left half (including mid)
else
high = mid;
}
// low == high points to the minimum element
return arr[low];
}
public static void main(String[] args) {
int[] arr = {5, 6, 1, 2, 3, 4};
System.out.print(findMin(arr));
}
}
def findMin(arr):
low, high = 0, len(arr) - 1
while low < high:
# If current range is sorted, first element is minimum
if arr[low] < arr[high]:
return arr[low]
mid = low + (high - low) // 2
# Minimum lies in right half
if arr[mid] > arr[high]:
low = mid + 1
# Minimum lies in left half (including mid)
else:
high = mid
# low == high points to the minimum element
return arr[low]
if __name__ == "__main__":
arr = [5, 6, 1, 2, 3, 4]
print(findMin(arr))
using System;
class GfG
{
static int findMin(int[] arr)
{
int low = 0, high = arr.Length - 1;
while (low < high)
{
// If current range is sorted, first element is minimum
if (arr[low] < arr[high])
return arr[low];
int mid = low + (high - low) / 2;
// Minimum lies in right half
if (arr[mid] > arr[high])
low = mid + 1;
// Minimum lies in left half (including mid)
else
high = mid;
}
// low == high points to the minimum element
return arr[low];
}
static void Main()
{
int[] arr = { 5, 6, 1, 2, 3, 4 };
Console.Write(findMin(arr));
}
}
function findMin(arr) {
let low = 0, high = arr.length - 1;
while (low < high) {
// If current range is sorted, first element is minimum
if (arr[low] < arr[high])
return arr[low];
let mid = Math.floor(low + (high - low) / 2);
// Minimum lies in right half
if (arr[mid] > arr[high])
low = mid + 1;
// Minimum lies in left half (including mid)
else
high = mid;
}
// low == high points to the minimum element
return arr[low];
}
const arr = [5, 6, 1, 2, 3, 4];
console.log(findMin(arr));
Output
1