Skip to content

Instantly share code, notes, and snippets.

@jnhemant
Created August 31, 2020 09:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnhemant/3dc0c38767218be65100058617d2660d to your computer and use it in GitHub Desktop.
Save jnhemant/3dc0c38767218be65100058617d2660d to your computer and use it in GitHub Desktop.
Coding challenge
/*Defuse Bomb
During the war, the enemy battalion has planted a bomb in your bunker. Your informer has sent you a message of the enemy which contains a list having N numbers and key(K) . The numbers have to be used to construct a sequence to diffuse the bomb. According to your informer, the logic to extract the sequence from the whole message is to replace each number with the sum of the next k elements , if the value of k is positive. When the value of K is negative, the number replaced by the sum of previous K numbers. The series of numbers is considered in a cyclic fashion for the last K numbers
Write an algorithm that finds the sequence to defuse the bomb
Input Format
Input to the function consist of three arguments
size , no of elements in the sequence
key , represents the K value
message, represents the sequence
Constraints
0<= size <= 10^5
-10^6 <= message[i] <= 10^6
0<= i <= size
Output Format
Return an array of integers representing the sequence to diffuse the bomb
Sample Input 0
4 3
4 2 -5 11
Sample Output 0
8 10 17 1*/
public void foo(int n. int key, int arr[]){
int c = key,sum = 0,b = 0;
for(int i = 0; i < n; i++){
b = i;
c = key;
sum = 0;
if(key > 0){
while(c > 0){
sum += a[(b+1) % n];
b++;
c--;
}
}
else{
while(c < 0){
if(b <= 0){
b = b+n;
}
sum += a[(b - 1)];
b--;
c++;
}
}
System.out.print(sum + " ");
}
}
Time complexity: O(n*key)
Space complexity: O(n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment