|
| 1 | +--- |
| 2 | +id: defuse-the-bomb |
| 3 | +title: Defuse the Bomb Solution |
| 4 | +sidebar_label: 1652-Defuse the Bomb |
| 5 | +tags: |
| 6 | + - Circular Array |
| 7 | + - Sliding Window |
| 8 | + - LeetCode |
| 9 | + - Java |
| 10 | + - Python |
| 11 | + - C++ |
| 12 | +description: "This is a solution to the Defuse the Bomb problem on LeetCode." |
| 13 | +sidebar_position: 2 |
| 14 | +--- |
| 15 | + |
| 16 | +In this tutorial, we will solve the Defuse the Bomb problem using a circular array and sliding window approach. We will provide the implementation of the solution in C++, Java, and Python. |
| 17 | + |
| 18 | +## Problem Description |
| 19 | + |
| 20 | +You have a bomb to defuse, and 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 as follows: |
| 21 | + |
| 22 | +- If `k > 0`, replace the `i-th` number with the sum of the next `k` numbers. |
| 23 | +- If `k < 0`, replace the `i-th` number with the sum of the previous `k` numbers. |
| 24 | +- If `k == 0`, replace the `i-th` number with 0. |
| 25 | + |
| 26 | +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]`. |
| 27 | + |
| 28 | +### Examples |
| 29 | + |
| 30 | +**Example 1:** |
| 31 | + |
| 32 | +``` |
| 33 | +Input: code = [5,7,1,4], k = 3 |
| 34 | +Output: [12,10,16,13] |
| 35 | +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. |
| 36 | +``` |
| 37 | + |
| 38 | +**Example 2:** |
| 39 | + |
| 40 | +``` |
| 41 | +Input: code = [1,2,3,4], k = 0 |
| 42 | +Output: [0,0,0,0] |
| 43 | +Explanation: When k is zero, the numbers are replaced by 0. |
| 44 | +``` |
| 45 | + |
| 46 | +**Example 3:** |
| 47 | + |
| 48 | +``` |
| 49 | +Input: code = [2,4,9,3], k = -2 |
| 50 | +Output: [12,5,6,13] |
| 51 | +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. |
| 52 | +``` |
| 53 | + |
| 54 | +### Constraints |
| 55 | + |
| 56 | +- `n == code.length` |
| 57 | +- `1 <= n <= 100` |
| 58 | +- `1 <= code[i] <= 100` |
| 59 | +- `-(n - 1) <= k <= n - 1` |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +## Solution for Defuse the Bomb Problem |
| 64 | + |
| 65 | +### Intuition and Approach |
| 66 | + |
| 67 | +The problem can be solved using a sliding window approach to handle the circular nature of the array. Depending on the value of `k`, we sum the appropriate elements and use modular arithmetic to handle the circular behavior. |
| 68 | + |
| 69 | +<Tabs> |
| 70 | +<tabItem value="Brute Force" label="Brute Force"> |
| 71 | + |
| 72 | +### Approach 1: Brute Force (Naive) |
| 73 | + |
| 74 | +The brute force approach involves iterating through each element and calculating the sum of the next or previous `k` elements based on the value of `k`. |
| 75 | + |
| 76 | +#### Code in Different Languages |
| 77 | + |
| 78 | +<Tabs> |
| 79 | +<TabItem value="C++" label="C++" default> |
| 80 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 81 | + |
| 82 | +```cpp |
| 83 | +class Solution { |
| 84 | +public: |
| 85 | + vector<int> decrypt(vector<int>& code, int k) { |
| 86 | + int n = code.size(); |
| 87 | + vector<int> result(n, 0); |
| 88 | + |
| 89 | + if (k == 0) return result; |
| 90 | + |
| 91 | + for (int i = 0; i < n; ++i) { |
| 92 | + int sum = 0; |
| 93 | + for (int j = 1; j <= abs(k); ++j) { |
| 94 | + if (k > 0) { |
| 95 | + sum += code[(i + j) % n]; |
| 96 | + } else { |
| 97 | + sum += code[(i - j + n) % n]; |
| 98 | + } |
| 99 | + } |
| 100 | + result[i] = sum; |
| 101 | + } |
| 102 | + |
| 103 | + return result; |
| 104 | + } |
| 105 | +}; |
| 106 | +``` |
| 107 | +
|
| 108 | +</TabItem> |
| 109 | +<TabItem value="Java" label="Java"> |
| 110 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 111 | +
|
| 112 | +```java |
| 113 | +class Solution { |
| 114 | + public int[] decrypt(int[] code, int k) { |
| 115 | + int n = code.length; |
| 116 | + int[] result = new int[n]; |
| 117 | + |
| 118 | + if (k == 0) return result; |
| 119 | + |
| 120 | + for (int i = 0; i < n; ++i) { |
| 121 | + int sum = 0; |
| 122 | + for (int j = 1; j <= Math.abs(k); ++j) { |
| 123 | + if (k > 0) { |
| 124 | + sum += code[(i + j) % n]; |
| 125 | + } else { |
| 126 | + sum += code[(i - j + n) % n]; |
| 127 | + } |
| 128 | + } |
| 129 | + result[i] = sum; |
| 130 | + } |
| 131 | + |
| 132 | + return result; |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +</TabItem> |
| 138 | +<TabItem value="Python" label="Python"> |
| 139 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 140 | + |
| 141 | +```python |
| 142 | +class Solution: |
| 143 | + def decrypt(self, code: List[int], k: int) -> List[int]: |
| 144 | + n = len(code) |
| 145 | + result = [0] * n |
| 146 | + |
| 147 | + if k == 0: |
| 148 | + return result |
| 149 | + |
| 150 | + for i in range(n): |
| 151 | + sum_val = 0 |
| 152 | + for j in range(1, abs(k) + 1): |
| 153 | + if k > 0: |
| 154 | + sum_val += code[(i + j) % n] |
| 155 | + else: |
| 156 | + sum_val += code[(i - j + n) % n] |
| 157 | + result[i] = sum_val |
| 158 | + |
| 159 | + return result |
| 160 | +``` |
| 161 | + |
| 162 | +</TabItem> |
| 163 | +</Tabs> |
| 164 | + |
| 165 | +#### Complexity Analysis |
| 166 | + |
| 167 | +- Time Complexity: $O(n \cdot |k|)$ due to nested loops. |
| 168 | +- Space Complexity: $O(n)$ for the result array. |
| 169 | +- Where `n` is the length of the code array. |
| 170 | +- The time complexity is $O(n \cdot |k|)$ because we iterate through each element and calculate the sum of `k` elements. |
| 171 | +- The space complexity is $O(n)$ because we store the result in a new array. |
| 172 | + |
| 173 | +</tabItem> |
| 174 | +<tabItem value="Optimized" label="Optimized"> |
| 175 | + |
| 176 | +### Approach 2: Sliding Window (Optimized) |
| 177 | + |
| 178 | +The sliding window approach uses a more efficient way to calculate the sum by maintaining a running sum and updating it as the window slides over the array. |
| 179 | + |
| 180 | +#### Code in Different Languages |
| 181 | + |
| 182 | +<Tabs> |
| 183 | +<TabItem value="C++" label="C++" default> |
| 184 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 185 | + |
| 186 | +```cpp |
| 187 | +class Solution { |
| 188 | +public: |
| 189 | + vector<int> decrypt(vector<int>& code, int k) { |
| 190 | + int n = code.size(); |
| 191 | + vector<int> result(n, 0); |
| 192 | + |
| 193 | + if (k == 0) return result; |
| 194 | + |
| 195 | + int start = k > 0 ? 1 : k; |
| 196 | + int end = k > 0 ? k : -1; |
| 197 | + |
| 198 | + int sum = 0; |
| 199 | + for (int i = start; i != end + 1; ++i) { |
| 200 | + sum += code[(i + n) % n]; |
| 201 | + } |
| 202 | + |
| 203 | + for (int i = 0; i < n; ++i) { |
| 204 | + result[i] = sum; |
| 205 | + sum -= code[(start + i + n) % n]; |
| 206 | + sum += code[(end + 1 + i + n) % n]; |
| 207 | + } |
| 208 | + |
| 209 | + return result; |
| 210 | + } |
| 211 | +}; |
| 212 | +``` |
| 213 | +
|
| 214 | +</TabItem> |
| 215 | +<TabItem value="Java" label="Java"> |
| 216 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 217 | +
|
| 218 | +```java |
| 219 | +class Solution { |
| 220 | + public int[] decrypt(int[] code, int k) { |
| 221 | + int n = code.length; |
| 222 | + int[] result = new int[n]; |
| 223 | + |
| 224 | + if (k == 0) return result; |
| 225 | + |
| 226 | + int start = k > 0 ? 1 : k; |
| 227 | + int end = k > 0 ? k : -1; |
| 228 | + |
| 229 | + int sum = 0; |
| 230 | + for (int i = start; i != end + 1; ++i) { |
| 231 | + sum += code[(i + n) % n]; |
| 232 | + } |
| 233 | + |
| 234 | + for (int i = 0; i < n; ++i) { |
| 235 | + result[i] = sum; |
| 236 | + sum -= code[(start + i + n) % n]; |
| 237 | + sum += code[(end + 1 + i + n) % n]; |
| 238 | + } |
| 239 | + |
| 240 | + return result; |
| 241 | + } |
| 242 | +} |
| 243 | +``` |
| 244 | + |
| 245 | +</TabItem> |
| 246 | +<TabItem value="Python" label="Python"> |
| 247 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 248 | + |
| 249 | +```python |
| 250 | +class Solution: |
| 251 | + def decrypt(self, code: List[int], k: int) -> List[int]: |
| 252 | + n = len(code) |
| 253 | + result = [0] * n |
| 254 | + |
| 255 | + if k == 0: |
| 256 | + return result |
| 257 | + |
| 258 | + start, end = (1, k) if k > 0 else (k, -1) |
| 259 | + |
| 260 | + sum_val = sum(code[i % n] for i in range(start, end + 1)) |
| 261 | + |
| 262 | + for i in range(n): |
| 263 | + result[i] = sum_val |
| 264 | + sum_val -= code[(start + i) % n] |
| 265 | + sum_val += code[(end + 1 + i) % n] |
| 266 | + |
| 267 | + return result |
| 268 | +``` |
| 269 | + |
| 270 | +</TabItem> |
| 271 | +</Tabs> |
| 272 | + |
| 273 | +#### Complexity Analysis |
| 274 | + |
| 275 | +- Time Complexity: $O(n)$ due to the sliding window. |
| 276 | +- Space Complexity: $O(n)$ for the result array. |
| 277 | +- Where `n` is the length of the code array. |
| 278 | +- The time complexity is $O(n |
| 279 | + |
| 280 | +)$ because we iterate through each element once with a running sum. |
| 281 | +- The space complexity is $O(n)$ because we store the result in a new array. |
| 282 | + |
| 283 | +</tabItem> |
| 284 | +</Tabs> |
| 285 | + |
| 286 | +--- |
| 287 | + |
| 288 | +<h2>Authors:</h2> |
| 289 | + |
| 290 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 291 | +{['ImmidiSivani'].map(username => ( |
| 292 | + <Author key={username} username={username} /> |
| 293 | +))} |
| 294 | +</div> |
0 commit comments