Defuse the Bomb
You have a bomb to defuse, and your time is running out! Your informer will provide you with a circular array code of length n and a key k. To decrypt the code, you must replace every number simultaneously as follows: If k > 0, replace the ith number with the sum of the next k numbers. If k < 0, replace the ith number with the sum of the previous k numbers. If k == 0, replace the ith number with 0. As code is circular, the next element of code[n - 1] is code0, and the previous element of code0 is code[n - 1]. Given the circular array code and the key k, return the decrypted code. This is a fixed length window sliding across the array, just wrapping around to the front once it reaches the end.
Constraints
- n == code.length
- 2 ≤ n ≤ 100
- -(n - 1) ≤ k ≤ n - 1
Example
code = [5, 7, 1, 4], k = 3[12, 10, 16, 13]Explanation Index 0: 7 + 1 + 4 = 12. Index 1: 1 + 4 + 5 = 10 (wraps). Index 2: 4 + 5 + 7 = 16 (wraps). Index 3: 5 + 7 + 1 = 13 (wraps).
In plain terms
- Circular array
- An array where the last element wraps around to connect back to the first — so "the next element" after the final position lands you back at index 0.
The row is the code array, with a window of 3 sliding around it in a circle
Index 0: sum of next 3 = code[1] + code[2] + code[3] = 7 + 1 + 4 = 12.
What happens in this step
index 0, k = 3 sum = code[1] + code[2] + code[3] = 7 + 1 + 4 = 12 All three offsets (1, 2, 3) stay within bounds — no wrap needed yet.
Steps to visualize
- The row below is the code array and never changes. The highlight box shows which cells are being added up.
- The box can only be drawn as one unbroken stretch, so when the window wraps past the end it is drawn across the whole row — the note says which single cell is left out.
- For k > 0, each position sums the k numbers that follow it, wrapping to the front when needed.
- Start at index 0 and sum the next k numbers using modulo indexing.
- Move to the next index and sum its own next k numbers the same way.
- Continue until every index has its sum computed.
- Watch how the window wraps past the last index back to index 0.
Walk through the code
Same walkthrough, now with the code. Press Next to move one step and watch which lines run.
Index 0: sum of next 3 = code[1] + code[2] + code[3] = 7 + 1 + 4 = 12.
What happens in this step
index 0, k = 3 sum = code[1] + code[2] + code[3] = 7 + 1 + 4 = 12 All three offsets (1, 2, 3) stay within bounds — no wrap needed yet.
Solution
function decrypt(code, k) {
const n = code.length;
if (k === 0) {
return new Array(n).fill(0);
}
const windowSize = Math.abs(k);
const result = new Array(n).fill(0);
for (let i = 0; i < n; i++) {
let sum = 0;
for (let step = 1; step <= windowSize; step++) {
const offset = k > 0 ? step : -step;
const idx = ((i + offset) % n + n) % n;
sum += code[idx];
}
result[i] = sum;
}
return result;
}- Time
- O(n * |k|)
- Space
- O(n)
Test cases
| Input | Expected | Covers |
|---|---|---|
code = [5, 7, 1, 4], k = 3 | [12, 10, 16, 13] | example from the docstring |
code = [5, 7, 1, 4], k = -2 | [5, 9, 12, 8] | negative k sums the previous |k| numbers |
code = [1, 2, 3], k = 0 | [0, 0, 0] | k === 0 replaces every element with 0 |
code = [1, 2, 3, 4, 5], k = 4 | [14, 13, 12, 11, 10] | k equal to array length minus 1: window wraps almost all the way around |
code = [1, 2], k = 1 | [2, 1] | smallest valid input: two elements |
code = [2, 4, 9, 3], k = 2 | [13, 12, 5, 6] | larger, hand-verified case |