Defuse the Bomb (LeetCode 1652): Solution and Explanation

Easy
Array
Sliding window

Replace each number in a circular array with the sum of the next k (or previous -k) numbers. Every position's window is the previous one shifted by a single step around the circle, so a running sum computed with indices mod n handles all of them in one pass.

Intuition

Picture the numbers on a clock face. For k > 0, position i looks at the k numbers just clockwise of it; for k < 0, at the -k numbers just counter-clockwise.

Moving from position i to i + 1 rotates that window by one: the element nearest to the old position falls out and the next element on the far side comes in.

So sum the first window once, then for every position write the current sum and update it with one subtraction and one addition. Taking indices mod n makes the wrap-around free.

Approach: Circular sliding window

  1. If k == 0, return an array of n zeros.
  2. Set the window for position 0: start = 1, end = k when k > 0, or start = n + k, end = n - 1 when k < 0.
  3. Sum code[j % n] for j from start to end.
  4. For each i from 0 to n - 1: set res[i] = window, subtract code[(start + i) % n], add code[(end + i + 1) % n].
  5. Return res.

Step-by-step example

code = [5,7,1,4], k = 3
  1. Window for position 0 is indices 1..3: 7 + 1 + 4 = 12. res[0] = 12.
  2. Drop code[1] = 7, add code[4 % 4] = 5: window = 10. res[1] = 10.
  3. Drop code[2] = 1, add code[1] = 7: window = 16. res[2] = 16.
  4. Drop code[3] = 4, add code[2] = 1: window = 13. res[3] = 13.
  5. Answer: [12,10,16,13].

Code: Python, Java, C++

1class Solution:
2    def decrypt(self, code: List[int], k: int) -> List[int]:
3        n = len(code)
4        res = [0] * n
5        if k == 0:
6            return res
7        start, end = (1, k) if k > 0 else (n + k, n - 1)
8        window = sum(code[j % n] for j in range(start, end + 1))
9        for i in range(n):
10            res[i] = window
11            window -= code[(start + i) % n]
12            window += code[(end + i + 1) % n]
13        return res
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

One pass to build the first window (at most n - 1 elements) and one pass with O(1) work per position.

Space

O(n)

The result array; the window itself needs O(1).

Other approaches

Direct sum per position (time O(n * |k|), space O(n))

For every i, loop over its |k| neighbours and add them up. With n at most 100 this is fast enough, but it repeats work.

Prefix sums over a doubled array (time O(n), space O(n))

Concatenate code with itself, build prefix sums, and read each window as a difference. Avoids mod arithmetic at the cost of an extra 2n array.

Common mistakes

  • Updating code in place. Later positions need the original values, so write into a separate result array.
  • Negative indices with k < 0. In most languages -1 % n is -1, so add n before taking the modulus, or start the window at n + k as above.
  • Forgetting the k == 0 case, where the window is empty and the bounds formula would produce a bogus range.
  • Off-by-one when sliding: the element leaving is start + i and the element entering is end + i + 1.

Frequently asked questions

Why does the window for k < 0 start at n + k?

Position 0's previous -k elements are the last -k elements of the array, indices n + k through n - 1. From there the window slides forward exactly like the k > 0 case.

Can the window wrap past the end more than once?

No. |k| is at most n - 1, so a window never covers the whole circle and never includes its own position.

Why not just loop over the k neighbours for each position?

That works under these constraints, but the sliding window is the same idea in O(n) and scales to much larger inputs.

What is the time and space complexity?

O(n) time and O(n) space for the output array.