Defuse the Bomb
Real-world version: Rewriting the Dial RingThe 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
shiftslots clockwise, whenshift > 0; - the sum of the numbers on the previous
-shiftslots anticlockwise, whenshift < 0; 0, whenshift == 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.
def rewrite_ring(ring: list[int], shift: int) -> list[int]:public int[] rewriteRing(int[] ring, int shift)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Contains Duplicate IIReal-world version: Nearby Repeat TrimEasy
- Maximum Average Subarray IReal-world version: Warmest Run of DaysEasy
- Longest Harmonious SubsequenceReal-world version: Widest One-Degree SampleEasy
- Minimum Difference Between Highest and Lowest of K ScoresReal-world version: The Tightest Choice of k ReadingsEasy