> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Defuse the Bomb Python Solution with Tests

> Tested Python solution for LeetCode 1652 with 26 pytest cases. Generate a practice environment with lcpy.

LeetCode 1652, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Sliding Window](/catalog/topics/sliding-window). [View on LeetCode](https://leetcode.com/problems/defuse-the-bomb/description/).

Generate this problem as a practice environment: tested reference solution, 26 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1652   # by problem number
lcpy gen -s defuse_the_bomb   # by problem name
```

## 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 of `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 `i<sup>th</sup>` number with the sum of the **next** `k` numbers.
* If `k < 0`, replace the `i<sup>th</sup>` number with the sum of the **previous** `-k` numbers.
* If `k == 0`, replace the `i<sup>th</sup>` 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*!

### Examples

```
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.
```

```
Input: code = [1,2,3,4], k = 0
Output: [0,0,0,0]
Explanation: When k is zero, the numbers are replaced by 0.
```

```
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.length
* 1 \<= n \<= 100
* 1 \<= code\[i] \<= 100
* -(n - 1) \<= k \<= n - 1

**Follow up:** Could you solve it in `O(n)` time without scanning the window from scratch for every index?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/defuse_the_bomb/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/defuse_the_bomb/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1) extra (output excluded)
    def decrypt(self, code: list[int], k: int) -> list[int]:
        n = len(code)
        if k == 0:
            return [0] * n
        window = abs(k)
        offset = 1 if k > 0 else -window
        total = sum(code[(offset + j) % n] for j in range(window))
        result = [0] * n
        for i in range(n):
            result[i] = total
            total += code[(i + offset + window) % n] - code[(i + offset) % n]
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) extra (output excluded) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.