Watch 10 video solutions for Defuse the Bomb, a easy level problem involving Array, Sliding Window. This walkthrough by NeetCodeIO has 14,888 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You have a bomb to defuse, and your time is running out! Your informer will provide you with a circular array code of length of n and a key k.
To decrypt the code, you must replace every number. All the numbers are replaced simultaneously.
k > 0, replace the ith number with the sum of the next k numbers.k < 0, replace the ith number with the sum of the previous k numbers.k == 0, replace the ith number with 0.As code is circular, the next element of code[n-1] is code[0], and the previous element of code[0] is code[n-1].
Given the circular array code and an integer key k, return the decrypted code to defuse the bomb!
Example 1:
Input: code = [5,7,1,4], k = 3 Output: [12,10,16,13] Explanation: Each number is replaced by the sum of the next 3 numbers. The decrypted code is [7+1+4, 1+4+5, 4+5+7, 5+7+1]. Notice that the numbers wrap around.
Example 2:
Input: code = [1,2,3,4], k = 0 Output: [0,0,0,0] Explanation: When k is zero, the numbers are replaced by 0.
Example 3:
Input: code = [2,4,9,3], k = -2 Output: [12,5,6,13] Explanation: The decrypted code is [3+9, 2+3, 4+2, 9+4]. Notice that the numbers wrap around again. If k is negative, the sum is of the previous numbers.
Constraints:
n == code.length1 <= n <= 1001 <= code[i] <= 100-(n - 1) <= k <= n - 1Problem Overview: You are given a circular array code and an integer k. Each element must be replaced by the sum of the next k elements if k > 0, the previous |k| elements if k < 0, or 0 if k == 0. Because the array is circular, indices wrap around the end.
The key challenge is handling the circular nature efficiently while computing multiple range sums.
Approach 1: Brute Force Iteration (O(n * |k|) time, O(n) space)
The straightforward solution iterates through every index and manually sums the required k neighbors. For each position i, run another loop that adds the next or previous |k| elements while applying modulo arithmetic to wrap around the array. This guarantees correctness and is easy to implement, especially when first understanding the circular behavior.
The downside is repeated work. Each element recomputes a sum from scratch, leading to O(n * |k|) time complexity. When k approaches n, performance degrades significantly. Space complexity remains O(n) because a separate result array is required. This approach mainly serves as a baseline before optimizing.
Approach 2: Two-Pointer Sliding Window Technique (O(n) time, O(1) extra space)
The optimal solution treats the required k elements as a sliding window over the circular array. Instead of recomputing each sum, maintain a running window sum and shift the window by one position at every step.
If k > 0, initialize a window covering indices 1 through k. If k < 0, build the window from n-|k| through n-1. After computing the first window sum, move both window boundaries forward using two pointers while updating the sum: subtract the element leaving the window and add the new element entering it. Use modulo arithmetic to keep indices within bounds of the circular array.
This technique avoids redundant summation. Each element enters and leaves the window exactly once, producing O(n) time complexity and O(1) auxiliary space beyond the output array. The pattern is a classic application of Sliding Window optimization on a circular Array. Managing the boundaries with two pointers makes the transitions predictable and efficient.
Recommended for interviews: Start by describing the brute force method to demonstrate understanding of the circular indexing rules. Then move to the sliding window optimization. Interviewers typically expect the O(n) two-pointer solution because it eliminates repeated work and shows familiarity with sliding window patterns applied to circular arrays.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Iteration | O(n * |k|) | O(n) | Useful as a baseline or when constraints are very small and clarity matters more than optimization. |
| Two-Pointer Sliding Window | O(n) | O(1) extra | Best choice for large arrays. Efficiently maintains a running sum while moving a circular window. |