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: 1

Input: arr[] = {2, 1}
Output: 1

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.

Output

Time Complexity: O(N)
Auxiliary Space: O(1)

--

--

Consulting Engineer @ MongoDB

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store