-
Notifications
You must be signed in to change notification settings - Fork 6
/
Solution.cs
43 lines (36 loc) · 1.29 KB
/
Solution.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using static System.Console;
class Solution
{
static void Main(String[] args)
{
//No need to capture number of squares in the choclate bar. I use array's length property instead.
ReadLine();
var s_temp = Console.ReadLine().Split(' ');
var choclateBarValues = Array.ConvertAll(s_temp, Int32.Parse);
var tokens_d = Console.ReadLine().Split(' ');
var birthday = Convert.ToInt32(tokens_d[0]);
var birthMonth = Convert.ToInt32(tokens_d[1]);
var result = Solve(choclateBarValues, birthday, birthMonth);
WriteLine(result);
}
static int Solve(int[] choclateBarValues, int birthday, int birthMonth)
{
var totalWays = 0;
if (choclateBarValues.Length >= birthMonth)
{
var barPartSum = 0;
for (int i = 0; i < birthMonth; i++)
barPartSum += choclateBarValues[i];
if (barPartSum == birthday)
totalWays++;
for (int i = 0; i < choclateBarValues.Length - birthMonth; i++)
{
barPartSum = barPartSum - choclateBarValues[i] + choclateBarValues[i + birthMonth];
if (barPartSum == birthday)
totalWays++;
}
}
return totalWays;
}
}