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.length1 <= n <= 1001 <= 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:
The goal of this algorithm is to transform the input array code into a new array result, based on the integer value k. The transformation depends on whether k is positive, negative, or zero.
1. Initialization
- First, we create a new array named
resultthat has the same length as the input arraycode. - This array will store the computed values for each index after applying the transformation rule.
- Initially, all elements in
resultcan be set to0.
result = [0] * len(code)
This ensures we have a placeholder for every element we’ll later update.
2. Iterating Through Each Element
We loop through each index i of the input array code.
For every position i, we will determine which elements to sum based on the value of k.
3. Case 1: When k > 0
- This means for every element at position
i, we need to compute the sum of the nextkelements in the array. - The array is circular, so if we reach the end of the array, we wrap around to the beginning.
Example:
If code = [5, 7, 1, 4] and k = 2:
- For index
0: next two elements are[7, 1], sum =8 - For index
1: next two elements are[1, 4], sum =5 - For index
2: next two elements are[4, 5](wraps around), sum =9 - For index
3: next two elements are[5, 7], sum =12
To achieve wrapping, we use modular arithmetic (% n), where n is the length of code.
result[i] = sum(code[(i + j) % n] for j in range(1, k + 1))
4. Case 2: When k < 0
- This means we need to compute the sum of the previous
|k|elements for each index. - Again, since the array is circular, we can wrap around to the end of the array when needed.
Example:
If code = [5, 7, 1, 4] and k = -2:
- For index
0: previous two elements are[4, 1], sum =5 - For index
1: previous two elements are[5, 4], sum =9 - For index
2: previous two elements are[7, 5], sum =12 - For index
3: previous two elements are[1, 7], sum =8
This can also be implemented with modular arithmetic:
result[i] = sum(code[(i + j) % n] for j in range(k, 0))
Here, range(k, 0) generates negative offsets that move backward from index i.
5. Case 3: When k == 0
- If
kis zero, it means no elements before or after the current position should be considered. - Therefore, every element in
resultbecomes zero.
if k == 0:
return [0] * len(code)
6. Final Output
After processing all elements, the result array contains the transformed values according to the rules defined by k.
✅ Example Summary
Input (code) | k | Explanation | Output (result) |
|---|---|---|---|
[5, 7, 1, 4] | 2 | Sum of next 2 elements | [8, 5, 9, 12] |
[5, 7, 1, 4] | -2 | Sum of previous 2 elements | [5, 9, 12, 8] |
[5, 7, 1, 4] | 0 | Replace all with zeros | [0, 0, 0, 0] |