Defuse the Bomb (LeetCode 1652): Solution and Explanation
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
- If
k == 0, return an array ofnzeros. - Set the window for position 0:
start = 1, end = kwhenk > 0, orstart = n + k, end = n - 1whenk < 0. - Sum
code[j % n]forjfromstarttoend. - For each
ifrom 0 to n - 1: setres[i] = window, subtractcode[(start + i) % n], addcode[(end + i + 1) % n]. - Return
res.
Step-by-step example
code = [5,7,1,4], k = 3
- Window for position 0 is indices 1..3: 7 + 1 + 4 = 12. res[0] = 12.
- Drop code[1] = 7, add code[4 % 4] = 5: window = 10. res[1] = 10.
- Drop code[2] = 1, add code[1] = 7: window = 16. res[2] = 16.
- Drop code[3] = 4, add code[2] = 1: window = 13. res[3] = 13.
- 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 res1class Solution {
2 public int[] decrypt(int[] code, int k) {
3 int n = code.length;
4 int[] res = new int[n];
5 if (k == 0) {
6 return res;
7 }
8 int start = k > 0 ? 1 : n + k;
9 int end = k > 0 ? k : n - 1;
10 int window = 0;
11 for (int j = start; j <= end; j++) {
12 window += code[j % n];
13 }
14 for (int i = 0; i < n; i++) {
15 res[i] = window;
16 window -= code[(start + i) % n];
17 window += code[(end + i + 1) % n];
18 }
19 return res;
20 }
21}1class Solution {
2public:
3 vector<int> decrypt(vector<int>& code, int k) {
4 int n = code.size();
5 vector<int> res(n, 0);
6 if (k == 0) {
7 return res;
8 }
9 int start = k > 0 ? 1 : n + k;
10 int end = k > 0 ? k : n - 1;
11 int window = 0;
12 for (int j = start; j <= end; j++) {
13 window += code[j % n];
14 }
15 for (int i = 0; i < n; i++) {
16 res[i] = window;
17 window -= code[(start + i) % n];
18 window += code[(end + i + 1) % n];
19 }
20 return res;
21 }
22};Time and space complexity
O(n)
One pass to build the first window (at most n - 1 elements) and one pass with O(1) work per position.
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
codein place. Later positions need the original values, so write into a separate result array. - Negative indices with
k < 0. In most languages-1 % nis -1, so addnbefore taking the modulus, or start the window atn + kas above. - Forgetting the
k == 0case, where the window is empty and the bounds formula would produce a bogus range. - Off-by-one when sliding: the element leaving is
start + iand the element entering isend + 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.