Ctrl K

The Shift That Scores the Most

Hard
You get an array values of length n. You may rotate it left by k positions (0 ≤ k < n), turning it into [values[k], ..., values[n − 1], values[0], ..., values[k − 1]]. After the rotation, every element whose value is at most its new index earns one point. For example, [3, 0, 2, 1] earns 3 points: for 0 at index 1, 2 at index 2 and 1 at index 3. Return the shift k that earns the most points. If several shifts tie, return the smallest one.
Example 1
values=[2, 0, 1] → 1

Shift 0: [2, 0, 1] scores for 0 and 1, total 2. Shift 1: [0, 1, 2] scores for all three, total 3. Shift 2: [1, 2, 0] scores only for 0, total 1. Shift 1 wins.

Example 2
values=[0, 0] → 0

Both zeros score under any shift, so we take the smallest one, 0.

Example 3
values=[1, 2, 3, 0] → 3
Constraints
  • 1 ≤ n = len(values) ≤ 10⁵
  • 0 ≤ values[i] < n
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRichest Plot Under the Cap HardThe Heaviest Reading Day HardFour Equal Pieces HardBest Average of a Long Stretch HardShortest Stretch Reaching a Total Hard
loading Python…
Input
values=[2, 0, 1]
Expected
1

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