Program to decrypt code to defuse the bomb in Python

Suppose there is a bomb that you are going to defuse, and your time is running out! You have a circular array code of length n and have a key k. To decrypt the code, you must replace every number simultaneously based on these rules ?

  • If k > 0 then replace ith number with the sum of next k numbers.

  • If k < 0 then replace ith number with the sum of previous k numbers.

  • If k = 0 then replace ith number with 0.

Since the code is circular, the next element of code[n-1] is code[0], and the previous element of code[0] is code[n-1].

Example

If the input is code = [8,2,3,5] and k = 3, the output will be [10, 16, 15, 13] because ?

  • code[0] = 8 ? sum of next 3 elements: 2 + 3 + 5 = 10

  • code[1] = 2 ? sum of next 3 elements: 3 + 5 + 8 = 16 (wraps around)

  • code[2] = 3 ? sum of next 3 elements: 5 + 8 + 2 = 15 (wraps around)

  • code[3] = 5 ? sum of next 3 elements: 8 + 2 + 3 = 13 (wraps around)

Algorithm

To solve this problem, we follow these steps ?

  • Create an empty decode list to store results

  • For each position i in the code array:

    • If k > 0, sum the next k elements (using modulo for circular indexing)

    • If k = 0, append 0 to the result

    • If k < 0, sum the previous |k| elements (using modulo for circular indexing)

  • Return the decoded array

Python Implementation

def solve(code, k):
    decode = []
    n = len(code)
    
    for i in range(n):
        if k > 0:
            total = 0
            for j in range(1, k + 1):
                total += code[(i + j) % n]
            decode.append(total)
            
        elif k == 0:
            decode.append(0)
            
        else:  # k < 0
            total = 0
            for j in range(1, abs(k) + 1):
                total += code[(i - j) % n]
            decode.append(total)
    
    return decode

# Test the function
code = [8, 2, 3, 5]
k = 3
result = solve(code, k)
print(f"Code: {code}")
print(f"Key k: {k}")
print(f"Decrypted: {result}")
Code: [8, 2, 3, 5]
Key k: 3
Decrypted: [10, 16, 15, 13]

Testing Different Cases

Let's test with different values of k ?

def solve(code, k):
    decode = []
    n = len(code)
    
    for i in range(n):
        if k > 0:
            total = 0
            for j in range(1, k + 1):
                total += code[(i + j) % n]
            decode.append(total)
            
        elif k == 0:
            decode.append(0)
            
        else:  # k < 0
            total = 0
            for j in range(1, abs(k) + 1):
                total += code[(i - j) % n]
            decode.append(total)
    
    return decode

code = [5, 7, 1, 4]

# Test k > 0
print("k = 2:", solve(code, 2))

# Test k = 0
print("k = 0:", solve(code, 0))

# Test k < 0
print("k = -2:", solve(code, -2))
k = 2: [8, 5, 9, 12]
k = 0: [0, 0, 0, 0]
k = -2: [11, 6, 12, 8]

How It Works

The key insight is using modulo arithmetic (i + j) % n and (i - j) % n to handle the circular nature of the array. This ensures we wrap around correctly when accessing elements beyond array boundaries.

For negative indices in Python, (i - j) % n automatically handles the wraparound correctly, giving us the previous elements in circular fashion.

Conclusion

This bomb defusing algorithm uses circular array indexing with modulo arithmetic to decrypt codes based on summing neighboring elements. The solution handles all three cases of k values efficiently with O(nĂ—|k|) time complexity.

---
Updated on: 2026-03-25T20:25:37+05:30

659 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements