src/srinesh/algorithms/q16_subarray_division/Solution.java
<reponame>s1n7ax/hacker-rank-pair-programming<filename>src/srinesh/algorithms/q16_subarray_division/Solution.javaimport static java.util.stream.Collectors.toList;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

class Result {

    /*
     * Complete the 'birthday' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts following parameters:
     * 1. INTEGER_ARRAY s
     * 2. INTEGER d
     * 3. INTEGER m
     */

    public static int birthday(List<Integer> s, int d, int m) {
        var size = s.size();

        return (int) IntStream
                .range(0, size)
                .takeWhile(index -> index + m <= size)
                .boxed()
                .map((index) -> {
                    return s
                            .subList(index, index + m)
                            .stream()
                            .reduce(0, (acc, v) -> acc + v);
                })
                .map(i -> {
                    return i;
                })
                .filter((i) -> i == d)
                .count();

    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int n = Integer.parseInt(bufferedReader.readLine().trim());

        List<Integer> s = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                .map(Integer::parseInt)
                .collect(toList());

        String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");

        int d = Integer.parseInt(firstMultipleInput[0]);

        int m = Integer.parseInt(firstMultipleInput[1]);

        int result = Result.birthday(s, d, m);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}
