![]() |
VOOZH | about |
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
A simple solution is to use linear search to traverse the complete array and find a minimum.
1
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:
arr[low] < arr[high], the current range is already sorted, return arr[low].mid.arr[mid] > arr[high], the minimum lies to the right of mid, set low = mid + 1.mid or to its left, set high = mid.low == high, that index stores the minimum element.1