Defuse the Bomb

You have a bomb to defuse, and your time is running out! Your informer gives you a **circular** array `code` of length `n` and a key `k`. To decrypt the code, replace every number at the same time: - If `k > 0`, replace the `i`th number with the sum of the **next** `k` numbers. - If `k < 0`, replace the `i`th number with the sum of the **previous** `k` numbers. - If `k == 0`, replace the `i`th number with `0`. Because `code` is circular, the element after `code[n - 1]` is `code[0]` and the element before `code[0]` is `code[n - 1]`. Return the decrypted code.

30:00

Defuse the Bomb
easy
Free written solution
Topics
Companies

You have a bomb to defuse, and your time is running out! Your informer gives you a circular array code of length n and a key k.

To decrypt the code, replace every number at the same time:

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

Because code is circular, the element after code[n - 1] is code[0] and the element before code[0] is code[n - 1].

Return the decrypted code.

Example 1:
Input: {"code":[5,7,1,4],"k":3}
Output: [12,10,16,13]
Constraints:
  • n=code.lengthn = \text{code.length}

  • 1≤n≤1001 \leq n \leq 100

  • 1≤code[i]≤1001 \leq \text{code}[i] \leq 100

  • −(n−1)≤k≤n−1-(n - 1) \leq k \leq n - 1

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"code":[5,7,1,4],"k":3}

The code has 4 numbers in a circle. Each one becomes the sum of the 3 numbers after it, wrapping around the end.

circular code, k = 3 (next 3)

50711243

res (decrypted)

?

0

?

1

?

2

?

3

Variables
VariableValue
n4
k3
DepthFunction Call
Stack empty
0/10