From ba354ba522b00b1f4a4d82d6a3768cfb1f75bc84 Mon Sep 17 00:00:00 2001 From: Prasad Honrao Date: Tue, 11 Jun 2019 14:31:13 +0000 Subject: [PATCH] Add birthday chocolate java solution --- .../birthday-chocolate.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 problem-solving/algorithms/implementation/birthday-chocolate/birthday-chocolate.java diff --git a/problem-solving/algorithms/implementation/birthday-chocolate/birthday-chocolate.java b/problem-solving/algorithms/implementation/birthday-chocolate/birthday-chocolate.java new file mode 100644 index 0000000..f014feb --- /dev/null +++ b/problem-solving/algorithms/implementation/birthday-chocolate/birthday-chocolate.java @@ -0,0 +1,56 @@ +import java.io.*; +import java.math.*; +import java.security.*; +import java.text.*; +import java.util.*; +import java.util.concurrent.*; +import java.util.function.*; +import java.util.regex.*; +import java.util.stream.*; +import static java.util.stream.Collectors.joining; +import static java.util.stream.Collectors.toList; + +public class Solution { + + // Complete the birthday function below. + static int birthday(List s, int d, int m) { + int[] sums = new int[s.size() + 1]; + int count = 0; + + for(int i = 0; i < s.size(); i++) { + + int sumsIndex = i + 1; + sums[sumsIndex] = sums[sumsIndex - 1] + s.get(i); + + if (sumsIndex >= m && (sums[sumsIndex] - sums[sumsIndex - m]) == d) + count++; + } + + return count; + } + + 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 s = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) + .map(Integer::parseInt) + .collect(toList()); + + String[] dm = bufferedReader.readLine().replaceAll("\\s+$", "").split(" "); + + int d = Integer.parseInt(dm[0]); + + int m = Integer.parseInt(dm[1]); + + int result = birthday(s, d, m); + + bufferedWriter.write(String.valueOf(result)); + bufferedWriter.newLine(); + + bufferedReader.close(); + bufferedWriter.close(); + } +}