HardArrayBinary Search

1 approach, code in every language

Problem description

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

After the pattern

Now solve one you haven't seen.

Lowest Reading On A Turned Drum is our own problem on the technique behind Find Minimum in Rotated Sorted Array II: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.

1,194 problems · 15,062 graded tests · free