package leetcode;

/**
 * https://leetcode.com/problems/count-number-of-nice-subarrays/
 */
public class Problem1248 {
    public int numberOfSubarrays(int[] nums, int k) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1248 prob = new Problem1248();
        System.out.println(prob.numberOfSubarrays(new int[]{1,1,2,1,1}, 3)); // 2
        System.out.println(prob.numberOfSubarrays(new int[]{2,4,6}, 1)); // 0
        System.out.println(prob.numberOfSubarrays(new int[]{2,2,2,1,2,2,1,2,2,2}, 2)); // 16
        System.out.println(prob.numberOfSubarrays(new int[]{2,2,2,1,2,2,1,2,2,2,1,2,2}, 2)); // 25
        System.out.println(prob.numberOfSubarrays(new int[]{2,2,2,1,2,2,1,2,2,2,1,2,2,2}, 2)); // 28
    }
}

@@ -12,16 +12,68 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 /**%0A * h
@@ -230,38 +230,51 @@
     
-// TODO%0A        return
+int answer = 0;%0A        int count =
  0;%0A    
 %7D%0A%0A 
@@ -273,554 +273,912 @@
     
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem1248 prob = new Problem1248();%0A        System.out.println(prob.numberOfSubarrays(new int%5B%5D%7B1,1,2,1,1%7D, 3)); // 2%0A        System.out.println(prob.numberOfSubarrays(new int%5B%5D%7B2,4,6%7D, 1)); // 0%0A        System.out.println(prob.numberOfSubarrays(new int%5B%5D%7B2,2,2,1,2,2,1,2,2,2%7D, 2)); // 16%0A        System.out.println(prob.numberOfSubarrays(new int%5B%5D%7B2,2,2,1,2,2,1,2,2,2,1,2,2%7D, 2)); // 25%0A        System.out.println(prob.numberOfSubarrays(new int%5B%5D%7B2,2,2,1,2,2,1,2,2,2,1,2,2,2%7D, 2)); // 28
+int index = 0;%0A        List%3CInteger%3E indexes = new ArrayList%3C%3E();%0A        for (int i = 0; i %3C nums.length; i++) %7B%0A            if (nums%5Bi%5D %25 2 != 0) %7B%0A                count++;%0A                indexes.add(i);%0A            %7D%0A            if (count %3E k) %7B%0A                int left = index - 1 %3C 0 ? indexes.get(index) : indexes.get(index) - indexes.get(index - 1) - 1;%0A                int right = indexes.get(indexes.size() - 1) - indexes.get(indexes.size() - 2) - 1;%0A                index++;%0A                count--;%0A                answer += (1 + left + right + (left * right));%0A            %7D%0A        %7D%0A        if (count == k) %7B%0A            int left = index - 1 %3C 0 ? indexes.get(index) : indexes.get(index) - indexes.get(index - 1) - 1;%0A            int right = nums.length - indexes.get(indexes.size() - 1) - 1;%0A            answer += (1 + left + right + (left * right));%0A        %7D%0A        return answer;
 %0A   

Solve problem 1248