Ctrl K

Minimum of a Shifted Array With Repeats

Hard
An integer array sorted in non-decreasing order was rotated to the right by some number of positions between 1 and n: each step moves the last element to the front. For instance, [1, 3, 6, 8, 8] becomes [6, 8, 8, 1, 3] after three steps and returns to its original form after n steps. The array may contain repeated values. Given the resulting array values, return its smallest element using as few steps as you can. Think about it: how do repeats change the complexity compared with all-distinct elements?
Example 1
values=[6, 8, 8, 1, 3] → 1

The sorted array [1, 3, 6, 8, 8] was shifted by 3 positions; the minimum is 1.

Example 2
values=[4, 4, 4, 4, 2, 4] → 2

Because of the repeats, the ends and the middle all hold 4, which alone does not tell which half the minimum is in. The answer is 2.

Example 3
values=[-3, 0, 7] → -3

A shift by the full length brought the array back to its sorted form.

Constraints
  • 1 ≤ n = len(values) ≤ 5000
  • -5000 ≤ values[i] ≤ 5000
  • values is a sorted array rotated by 1 to n positions
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMedian Across Two Arrays HardFrame Around an Ink Blot HardSmaller Values to the Right HardWindow Sums Within a Band HardBoxes Inside Boxes Hard
loading Python…
Input
values=[6, 8, 8, 1, 3]
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 121 tests on the server, hidden ones included.