Show problems as
Classic problem #189MediumArrayMathTwo Pointers

Rotate Array

Real-world version: Delay the Sample Buffer
Tracked in this browser only
Read Rotate Array on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A firmware ring buffer holds one signed sensor delta per slot in the integer array readings. Slot 0 is the oldest slot and the buffer wraps, so the slot after the last one is slot 0 again.

A technician wants the whole trace pushed later by delay slots: whatever sits in a slot ends up delay slots further along, and anything that runs off the end reappears at the front. The value of delay can be 0, and it can also be larger than the number of slots, in which case the trace simply wraps more than once.

Rearrange readings itself, using no more than a constant amount of extra space on top of the buffer, then return readings.

Examples

Example 1

Input
readings = [5, -2, 9, 4], delay = 1
Output
[4, 5, -2, 9]

Every delta moves one slot later and the 4 that fell off the end lands in slot 0.

Example 2

Input
readings = [7, 8, -3], delay = 5
Output
[8, -3, 7]

Five pushes across three slots is one full wrap plus two more, so the outcome matches a push of two.

Example 3

Input
readings = [6, 1, 4], delay = 0
Output
[6, 1, 4]

Nothing is pushed, so the buffer comes back exactly as it arrived.

Constraints

  • 1 <= readings.length <= 10^5
  • -2^31 <= readings[i] <= 2^31 - 1
  • 0 <= delay <= 10^5
  • Only a constant amount of extra space may be used

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def delay_buffer(readings: list[int], delay: int) -> list[int]:
Java
public int[] delayBuffer(int[] readings, int delay)