Ctrl K

Sorted List With a Shift

Easy
A sensor logs readings in a ring: they were originally in non-decreasing order, but the log may have been read starting somewhere in the middle. In other words, a sorted list may have been rotated by some number of positions (possibly zero): a few leading elements moved to the end, keeping their order. Given the list readings, return True if it can be such a rotation of a list sorted in non-decreasing order, and False otherwise. Readings may repeat.
Example 1
readings=[6, 8, 8, 2, 3] → True

The original order was [2, 3, 6, 8, 8]; its beginning was simply moved to the end.

Example 2
readings=[5, 1, 4, 2] → False

Values drop both after 5 and after 4, which no shift of a sorted list can produce.

Example 3
readings=[1, 2, 9] → True

The list is already sorted, which is a shift by zero positions.

Constraints
  • 1 ≤ len(readings) ≤ 100
  • 1 ≤ readings[i] ≤ 100
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
readings=[6, 8, 8, 2, 3]
Expected
True

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