Skip to content

Instantly share code, notes, and snippets.

@caglarorhan
Created February 7, 2018 06:05
Show Gist options
  • Save caglarorhan/635bd2eb84eba09f04831ad44e4dc351 to your computer and use it in GitHub Desktop.
Save caglarorhan/635bd2eb84eba09f04831ad44e4dc351 to your computer and use it in GitHub Desktop.
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function solve(n, s, d, m){
// Complete this function
let cNx = s.toString().split(" ");
let cN = cNx[0].split(",");
let totalOptions=0;
//let str="";
for(var sEt=0; sEt<(n-(m-1));sEt++){
var summy = 0
//var sett="";
for(var mini=0; mini<m; mini++){
summy+=parseInt(cN[sEt+mini]);
//sett+=cN[sEt+mini]+'+';
}
//str+=sett+":"+summy+","
if(summy==d){totalOptions++;}
}
//return str;
return totalOptions;
}
function main() {
var n = parseInt(readLine());
s = readLine().split(' ');
s = s.map(Number);
var d_temp = readLine().split(' ');
var d = parseInt(d_temp[0]);
var m = parseInt(d_temp[1]);
var result = solve(n, s, d, m);
process.stdout.write(""+result+"\n");
}
@caglarorhan
Copy link
Author

Check here: https://www.hackerrank.com/challenges/the-birthday-bar/problem
Lily has a chocolate bar consisting of a row of squares where each square has an integer written on it. She wants to share it with Ron for his birthday, which falls on month and day . Lily wants to give Ron a piece of chocolate only if it contains consecutive squares whose integers sum to .

Given , , and the sequence of integers written on each square of Lily's chocolate bar, how many different ways can Lily break off a piece of chocolate to give to Ron?

For example, if , and the chocolate bar contains squares with the integers written on them from left to right, the following diagram shows two ways to break off a piece:

image

Input Format

The first line contains an integer denoting (the number of squares in the chocolate bar).
The second line contains space-separated integers describing the respective values of (the numbers written on each consecutive square of chocolate).
The third line contains two space-separated integers describing the respective values of (Ron's birth day) and (Ron's birth month).

Constraints

, where ()
Output Format

Print an integer denoting the total number of ways that Lily can give a piece of chocolate to Ron.

Sample Input 0

5
1 2 1 3 2
3 2
Sample Output 0

2
Explanation 0

This sample is already explained in the problem statement.

Sample Input 1

6
1 1 1 1 1 1
3 2
Sample Output 1

0
Explanation 1

Lily only wants to give Ron consecutive squares of chocolate whose integers sum to . There are no possible pieces satisfying these constraints:

image

Thus, we print as our answer.

Sample Input 2

1
4
4 1
Sample Output 2

1
Explanation 2

Lily only wants to give Ron square of chocolate with an integer value of . Because the only square of chocolate in the bar satisfies this constraint, we print as our answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment