CRAN04 - Audition

Penny is a terrible waitress and even worse actress, however recently she applied for a role in an upcoming TV series. Even though she thought she had no chance, she was called for an audition. She was very happy about it until she found out that her character in this new series will be a studious, high IQ girl named Megan. Producer told her that to get the role of Megan she had to prove that her mind can handle a bit of mathematics and reasoning. If she passed the test then she will be given the role of Megan. The test was as follow.

The people (Boys and Girls) who came for audition are standing in a line in a random order. Producer has to select exactly K boys for the show. So he asks Penny to tell how many ways can he select two numbers i and j such that the number of boys standing between these (including I and j) indexes is exactly K.

Penny desperately needs this role. Everybody knows that Penny is not a very smart and requests you to help her.



First line contains T – The number of test cases.

Next line contains space separated N and K.

N – The total number of boys and girls who came to audition.

K – The number of the boys who must be there between each (i, j) pair.

Next line contains a non-empty string consisting of '1' and '0'.

1 represents Boy.

0 represents Girl.



The number of (i, j) pairs such that the number of boys between index i and j, both inclusive is equal to K.










4 1


5 2


5 4


Output: 6

hide comments
aman224: 2017-03-17 08:41:59

O(N) solution without binary search is possible...
Beginners just need to think a bit harder

ricky99999: 2017-02-01 12:36:29

O(n) Solution->Use same logic as in SUBSEQ & CRAN02.

Chinmay Kousik: 2016-11-26 14:17:09

Someone please tell me what's wrong with the source code. I keep getting WA. O(n) soln. submission id: 18262942

rahulpadhy: 2016-10-15 12:17:08

Last edit: 2016-10-15 12:23:26
geoffreymace7: 2016-07-18 18:26:55

Number of pairs can exceed 10^9, lead me to several WAs.

Last edit: 2016-07-18 18:27:54
aspro: 2016-05-18 12:54:56

input the string TLE bcuz of that

SUBHAM SANGHAI: 2016-05-16 17:17:20

nice Binary Search questn . Beware of the case when k=0

Last edit: 2016-05-16 17:27:34
785227: 2015-11-21 16:28:45

@BITMEN, It is tagged by someone who solved it with Binary Search.

[BITMEN] DARK LORD: 2015-09-16 20:12:29

The aim of the problem is to find the O(n) solution i.e DP solution.Don't know why it is tagged as binary search problem. Otherwise good problem with strong test cases.
HINT: store your result in long long . It costs me 3 wrong answer.

vedaytas: 2015-06-27 08:14:44

nice one.easy problem.look out for corner cases .good test constraints carefully!!

Added by:CSI
Time limit:2s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64