Show problems as
Classic problem #1652EasyArraySliding Window

Defuse the Bomb

Real-world version: Rewriting the Dial Ring
Tracked in this browser only
Read Defuse the Bomb on LeetCode(opens in a new tab)

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

A mechanical dial carries n numbered slots arranged in a circle; ring[i] is the number engraved on slot i, and slot n - 1 sits immediately before slot 0 going clockwise. A service manual asks you to re-engrave the whole dial at once, using a single setting shift.

For every slot i, its new number is:

  • the sum of the numbers on the next shift slots clockwise, when shift > 0;
  • the sum of the numbers on the previous -shift slots anticlockwise, when shift < 0;
  • 0, when shift == 0.

The counting wraps round the circle, and a slot never counts itself. Every new number is worked out from the numbers engraved before the rewrite, not from numbers written during it.

Return the list of new numbers, slot 0 first.

Examples

Example 1

Input
ring = [5, 7, 1, 4], shift = 2
Output
[8, 5, 9, 12]

Slot 0 reads slots 1 and 2, giving 7 + 1 = 8. Slot 1 reads slots 2 and 3, giving 1 + 4 = 5. Slot 2 wraps to slots 3 and 0, giving 4 + 5 = 9, and slot 3 wraps to slots 0 and 1, giving 5 + 7 = 12.

Example 2

Input
ring = [5, 7, 1, 4], shift = -1
Output
[4, 5, 7, 1]

Each slot takes the single number behind it, so slot 0 wraps back to slot 3 and reads 4, while slots 1, 2 and 3 read 5, 7 and 1.

Example 3

Input
ring = [3, 9, 2], shift = 0
Output
[0, 0, 0]

A setting of zero engraves 0 on every slot.

Constraints

  • 1 <= ring.length <= 100
  • 1 <= ring[i] <= 100
  • -(n - 1) <= shift <= n - 1, where n is the number of slots.

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 rewrite_ring(ring: list[int], shift: int) -> list[int]:
Java
public int[] rewriteRing(int[] ring, int shift)