Leet code problem: 1652. Defuse the Bomb

Description:

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

Given the circular array code and an integer key k, return the decrypted code to defuse the bomb!

Example 1:

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.

Example 2:

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

Example 3:

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

Solution:

class Solution {
    public int[] decrypt(int[] code, int k) {
        int len = code.length;
        int[] result = new int[len];
        int sum;

        for(int i=0;i<len;i++){
            if(k>0){
              sum= getSum(code,i+1,k,len,"right");
              result[i] = sum;
            }
            else if(k<0){
              sum= getSum(code,i-1,Math.abs(k),len,"left");
              result[i] = sum;
            }
            else{
              result[i] = 0;
            }
              
        }
        return result;
    }

    int getSum(int[] code, int i, int k, int len, String direction){
        int sum = 0;
        int index;
        if(direction.equals("right")){
            for(int j=i;j<i+k;j++){
              if(j>len-1){
                index = j-len;
              }
              else{
                index = j;
              }
              sum+=code[index];
            }
        }
        else{
            for(int j=i;j>i-k;j--){
              if(j<0){
                index = j+len;
              }
              else{
                index = j;
              }
              sum+=code[index];
            }
        }
        return sum;
    }
}
Approach:
🔍 Method Description: decrypt(int[] code, int k)
This method decrypts an array of integers (code) based on a key k. For each element in the array, it replaces the value with the sum of the next or previous k elements, depending on the sign of k, treating the array as circular.

⚙️ How It Works:
Create an array result of the same length as code to store the decrypted values.

Loop through each index i of the code array:

If k > 0:

Sum the next k elements (moving right) from index i+1, wrapping around if needed.

If k < 0:

Sum the previous |k| elements (moving left) from index i-1, wrapping around if needed.

If k == 0:

Set the value at index i in result to 0 (no elements to sum).

Use a helper function getSum to compute the sum of the circular window either to the left or right.

Return the resulting result array.

🔁 Helper Method: getSum(int[] code, int i, int k, int len, String direction)
This function calculates the sum of k elements starting from index i in the given direction ("right" or "left"):

Right direction: Adds the next k elements from index i, wrapping around the array if necessary.

Left direction: Adds the previous k elements from index i, wrapping around if needed.

Wraparound logic uses modular arithmetic:

If an index exceeds bounds (j >= len or j < 0), adjust using j - len or j + len.

📌 Example Use Case:
java
Copy
Edit
code = [5, 7, 1, 4]
k = 2
For index 0: next 2 elements are 7 and 1 → sum = 8

For index 1: next 2 elements are 1 and 4 → sum = 5

For index 2: next 2 elements are 4 and 5 (wraps) → sum = 9

For index 3: next 2 elements are 5 and 7 → sum = 12

So, result = [8, 5, 9, 12]
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Testingtalkslatest.com - A project by CreativeHub IT Solutions.
Contact Us At: support@testingtalkslatest.com
Our Partner websites - Classified Hub , CodesToolbox , CodesToolbox
Scroll to Top
0
Would love your thoughts, please comment.x
()
x