Ctrl K

Carousel Steps to Order

Easy
A baggage carousel belt carries n boxes with distinct labels: labels[i] is the label of the box at position i (positions are 0-based). In one step the belt moves: every box travels from position i to position i + 1, and the box at the last position n - 1 moves to position 0. Return the smallest number of steps after which the labels are strictly increasing from position 0 to position n - 1. If no number of steps achieves that, return -1. It is guaranteed that if the boxes are not already in ascending order, the label at the last position is smaller than the label at position 0.
Example 1
labels=[7, 9, 2, 4] → 2

After one step: [4, 7, 9, 2]; after the second: [2, 4, 7, 9], ascending.

Example 2
labels=[3, 8, 12] → 0

The boxes are already in ascending order, so no steps are needed.

Example 3
labels=[6, 1, 7, 2] → -1

The order breaks in two places (6 > 1 and 7 > 2), and moving the belt never changes the cyclic order of the boxes, so the answer is -1.

Constraints
  • 1 ≤ len(labels) ≤ 100
  • 1 ≤ labels[i] ≤ 100
  • All labels are distinct
  • If labels is not sorted, labels[n - 1] < labels[0]
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPair With a Target Sum EasySqueeze Repeats Out of a Sorted Array EasyDrop the Unwanted Value EasySlot on a Sorted Shelf EasyAdd One to a Digit Array Easy
loading Python…
Input
labels=[7, 9, 2, 4]
Expected
2

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