package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/defuse-the-bomb/
 */
public class Problem1652 {
    public int[] decrypt(int[] code, int k) {
        // TODO
        return null;
    }

    public static void main(String[] args) {
        Problem1652 prob = new Problem1652();
        System.out.println(Arrays.toString(prob.decrypt(new int[]{5,7,1,4}, 3))); // [12,10,16,13]
        System.out.println(Arrays.toString(prob.decrypt(new int[]{1,2,3,4}, 0))); // [0,0,0,0]
        System.out.println(Arrays.toString(prob.decrypt(new int[]{2,4,9,3}, -2))); // [12,5,6,13]
    }
}

@@ -16,34 +16,8 @@
 e;%0A%0A
-import java.util.Arrays;%0A%0A
 /**%0A
@@ -155,15 +155,66 @@
     
-// TODO
+int%5B%5D answer = new int%5Bcode.length%5D;%0A        if (k == 0) %7B
 %0A   
@@ -222,410 +222,1238 @@
     
+    
 return 
-null;%0A
+answer;%0A   
     
+ 
 %7D%0A
-%0A    public static void main(String%5B%5D args) %7B%0A        Problem1652 prob = new Problem1652();%0A        System.out.println(Arrays.toString(prob.decrypt(new int%5B%5D%7B5,7,1,4%7D, 3))); // %5B12,10,16,13%5D%0A        System.out.println(Arrays.toString(prob.decrypt(new int%5B%5D%7B1,2,3,4%7D, 0))); // %5B0,0,0,0%5D%0A        System.out.println(Arrays.toString(prob.decrypt(new int%5B%5D%7B2,4,9,3%7D, -2))); // %5B12,5,6,13%5D
+        int%5B%5D sums = new int%5Bcode.length%5D;%0A        for (int i = 0; i %3C code.length; i++) %7B%0A            if (i == 0) %7B%0A                sums%5Bi%5D = code%5Bi%5D;%0A            %7D else %7B%0A                sums%5Bi%5D = sums%5Bi - 1%5D + code%5Bi%5D;%0A            %7D%0A        %7D%0A        if (k %3E 0) %7B%0A            for (int i = 0; i %3C code.length; i++) %7B%0A                int start = i;%0A                int end = (i + k) %25 code.length;%0A                int val = getValue(sums, start, end);%0A                answer%5Bi%5D = val;%0A            %7D%0A        %7D else %7B%0A            for (int i = 0; i %3C code.length; i++) %7B%0A                int start = (i + k - 1) %25 code.length;%0A                if (start %3C 0) %7B%0A                    start = code.length + start;%0A                %7D%0A                int end = (i - 1) %25 code.length;%0A                if (end %3C 0) %7B%0A                    end = code.length + end;%0A                %7D%0A                int val = getValue(sums, start, end);%0A                answer%5Bi%5D = val;%0A            %7D%0A        %7D%0A        return answer;%0A    %7D%0A%0A    private static int getValue(int%5B%5D sums, int start, int end) %7B%0A        return end %3E start ?%0A            sums%5Bend%5D - sums%5Bstart%5D :%0A            sums%5Bsums.length - 1%5D - sums%5Bstart%5D + sums%5Bend%5D;
 %0A   

Solve problem 1652