


Let the array 'arr' be: [1, 0, 1].
Let ‘k’ be: 1
Then the subarrays having the number of ones equal to ‘k’ will be: [1], [1,0], [0,1], [1].
The first line contains two single space-separated integers 'N' and 'k'.
The second line contains 'N' space-separated integers, denoting the elements of array 'arr'.
The output contains the number of subarrays with the count of 1's equal to ‘k’.
You do not need to print anything, it has already been taken care of. Just implement the given function.
This approach is a brute-force solution to count the number of subarrays with a given sum 'k' in the binary array 'arr'. It uses nested loops to check all possible subarrays, resulting in a less efficient algorithm.
The algorithm uses a prefix sum array and a hashmap to count the number of subarrays with a given sum 'k' in a binary array 'arr'. It maintains a variable cnt to track the count of ones encountered so far and a variable res to store the count of subarrays. The prefix sum array prefix is used to keep track of the count of each prefix sum encountered during traversal. By comparing the current count cnt with the given sum 'k', the algorithm updates the result res by adding the count of subarrays that have the desired sum. Finally, it returns the count res as the answer.