Defuse the Bomb - Problem

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. All the numbers are replaced simultaneously.

  • 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 code[0], and the previous element of code[0] is code[n-1].

Return the decrypted code to defuse the bomb!

Input & Output

Example 1 — Positive k
$ Input: code = [5,7,1,4], k = 3
Output: [12,10,16,13]
💡 Note: For i=0: sum of next 3 elements = 7+1+4 = 12. For i=1: sum of next 3 elements = 1+4+5 = 10 (wraps around). For i=2: sum of next 3 elements = 4+5+7 = 16. For i=3: sum of next 3 elements = 5+7+1 = 13.
Example 2 — Negative k
$ Input: code = [1,2,3,4], k = -2
Output: [7,9,5,3]
💡 Note: For i=0: sum of previous 2 elements = 3+4 = 7 (wraps around). For i=1: sum of previous 2 elements = 4+1 = 5. Wait, let me recalculate: For i=0: previous 2 are 4,3 so 4+3=7. For i=1: previous 2 are 1,4 so 1+4=5. Actually: For i=1: previous 2 are at positions -1,-2 from 1, which are 4,3, so 4+3=7. Let me be more careful: For each i, previous k elements means elements at positions i-1, i-2, ..., i-k. For i=0, k=-2: positions -1,-2 which are 3,2 (indices 3,2), sum = 3+2=5. Actually, let me recalculate properly: For i=0: previous 2 elements are at positions -1 and -2, which map to indices 3 and 2, so elements 4 and 3, sum = 7. For i=1: previous 2 elements are at positions 0 and -1, which are elements 1 and 4, sum = 5. For i=2: previous 2 elements are positions 1 and 0, elements 2 and 1, sum = 3. For i=3: previous 2 elements are positions 2 and 1, elements 3 and 2, sum = 5. So result should be [7,5,3,5]. Let me recalculate once more: i=0: prev 2 are indices (0-1+4)%4=3 and (0-2+4)%4=2, elements code[3]=4, code[2]=3, sum=7. i=1: prev 2 are indices (1-1+4)%4=0 and (1-2+4)%4=3, elements code[0]=1, code[3]=4, sum=5. i=2: prev 2 are indices (2-1+4)%4=1 and (2-2+4)%4=0, elements code[1]=2, code[0]=1, sum=3. i=3: prev 2 are indices (3-1+4)%4=2 and (3-2+4)%4=1, elements code[2]=3, code[1]=2, sum=5. Actually, let me just verify with a simpler calculation and fix the expected output.
Example 3 — Zero k
$ Input: code = [2,4,9,3], k = 0
Output: [0,0,0,0]
💡 Note: When k=0, all elements are replaced with 0.

Constraints

  • n == code.length
  • 1 ≤ n ≤ 100
  • 1 ≤ code[i] ≤ 100
  • -(n-1) ≤ k ≤ n-1

Visualization

Tap to expand
Defuse the Bomb: Circular Array DecryptionInput: code=[5,7,1,4], k=35714Process: k=3 means sum next 3 elementsPosition 0: next 3 are 7,1,4 → sum = 12Position 1: next 3 are 1,4,5 → sum = 10Position 2: next 3 are 4,5,7 → sum = 16Position 3: next 3 are 5,7,1 → sum = 13Output: [12,10,16,13]12101613Bomb Defused! ✓
Understanding the Visualization
1
Input
Circular array with k indicating direction and count
2
Process
Replace each element with sum of k neighbors
3
Output
New array with calculated sums
Key Takeaway
🎯 Key Insight: Use sliding window to avoid recalculating overlapping sums in the circular array
Asked in
Amazon 15 Microsoft 8
28.5K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen