AlgoMaster Logo

Find Minimum in Rotated Sorted Array

Ashish

Ashish Pratap Singh

medium

Problem Description

Solve it on LeetCode

Approaches

Intuition:

When thinking about finding the minimum value in a rotated sorted array, a brute-force approach involves simply traversing the entire array and keeping track of the smallest element encountered. This is because a linear scan would allow us to identify the minimum element without any additional logic, albeit not efficiently.

Steps:

  • Traverse the array from the first to the last element.
  • Maintain a variable to store the current minimum element, initially set to the first element of the array.
  • For each element in the array, update the current minimum if the current element is less than the existing minimum.
  • By the end of the loop, the stored minimum will be the smallest element in the array.

Code:

Intuition:

The rotated sorted array still retains some properties of a sorted array. In a non-rotated sorted array, the first element is the smallest. Upon rotation, this property is only minimally affected. Hence, a more efficient approach can leverage binary search, exploiting the array's semi-sorted nature.

Steps:

  • Use two pointers, left and right, initialized to the start and end of the array respectively.
  • While left is less than right, calculate the middle index.
  • If the middle element is greater than the element at right, it means the minimum is to the right of mid.
  • If the middle element is less than or equal to the element at right, it means the minimum is at mid or to the left of mid.
  • Adjust the left or right pointers accordingly.
  • When the loop exits, left will point to the smallest element.

Code: