All problems
0662EasyArraySliding Window

Rewriting the Dial Ring

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1652Defuse the Bomb

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def rewrite_ring(ring: list[int], shift: int) -> list[int]:
Java
public int[] rewriteRing(int[] ring, int shift)
September 7
Apply