HardArrayBinary Search

Find Minimum in Rotated Sorted Array II

LeetCode
1 approach, code in all languages

An array that was originally sorted in ascending order has been rotated at some unknown pivot, and the array may contain duplicate values. After the rotation, find the smallest element in the array.

The presence of duplicates is what makes this variant harder: when the middle value ties with the boundary value, you cannot tell which half holds the minimum, so the search must degrade gracefully in those ambiguous cases.

Example 1

Input: nums = [4, 5, 6, 6, 1, 2, 3]

Output: 1

The array was rotated so that the ascending run wraps around; the smallest value sitting at the rotation point is 1.

Example 2

Input: nums = [3, 3, 1, 3, 3]

Output: 1

Duplicates surround the pivot, but narrowing the window carefully still isolates the minimum value of 1.

Constraints

  • 1 <= nums.length <= 5 * 10^4
  • -10^4 <= nums[i] <= 10^4
  • nums is a rotation of an array sorted in ascending order
  • nums may contain duplicate values
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now