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
vinayak1550: 2021-03-20 08:43:12

using ds which have logn tc may lead to tle.
use ds with o(1) tc

angry_any: 2020-09-14 00:58:51

Be careful of K=0. Depending on your solution, it will make you get wrong answer after 7th case

crypter472: 2020-08-03 13:12:38

take care of overflows :p

scolar_fuad: 2019-11-11 16:26:22

two pointer got AC
keep cosidaration when k=0 ..
happy coding

auhan: 2019-07-18 18:57:49

take care of the case with k=0 , and ans can be very large , nd there is a O(n) solution and i=j is a valid pair too

Last edit: 2019-07-18 18:59:33
blagojce: 2018-08-12 00:25:47

AC O(n) solution with 2 pointers

hello_world123: 2018-03-17 20:57:40

Using Dp but gettin WA in 7th test case ...
Can someone help with the corner cases?

Hugo Godoy: 2017-11-13 06:39:27

May I assume that the string has length N?

pradeep_yadav: 2017-09-02 09:47:16

did using binary search O(nlogn) solution.
Don't know how to solve in O(n).
Any Hint ??

hacker_sk: 2017-06-27 08:55:00

3rd rank with O(n) soln. :)

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