Count of Subarrays with sum equals k in given Binary Array
Last Updated :
13 Apr, 2024
Given a binary array arr[] and an integer k, the task is to find the count of non-empty subarrays with a sum equal to k.
Examples:
Input: arr[] = {1, 0, 1, 1, 0, 1}, k = 2
Output: 6
Explanation: All valid subarrays are: {1, 0, 1}, {0, 1, 1}, {1, 1}, {1, 0, 1}, {0, 1, 1, 0}, {1, 1, 0}.
Input: arr[] = {0, 0, 0, 0, 0}, k = 0
Output: 15
Explanation: All subarrays have a sum equal to 0, and there are a total of 15 subarrays.
Approach: This can be solved with the following idea:
We can solve this problem using a sliding window approach. We can keep track of the prefix sum of the array while iterating through it, and use two pointers to maintain a subarray with a sum equal to the given goal. We can calculate the number of subarrays with a maximum not greater than a goal and subtract the number of subarrays with a maximum not greater than goal-1 to get the final result.
Below are the steps involved in the implementation of the code:
- Count the number of subarrays having a sum at most k and k-1 by calling the function atmost().
- Return the difference in their count as a result.
Below is the implementation of the above approach:
C++
// C++ Implementation of code
#include <iostream>
#include <vector>
using namespace std;
// Function to find count of subarrays
// with sum at most k
int atmost(vector<int>& nums, int k)
{
if (k < 0)
return 0;
int l = 0, cnt = 0;
int res = 0;
for (int i = 0; i < nums.size(); i++) {
cnt += nums[i];
// Adjust the window sum by removing
// elements from the left until it is
// at most k
while (cnt > k && l<=i)
cnt -= nums[l++];
// Add the count of subarrays with
// sum at most k for the current window
res += (i - l + 1);
}
return res;
}
// Function to find count of subarrays
// with sum equal to k
int numSubarraysWithSum(vector<int>& nums, int goal)
{
// Call atmost(nums, goal) and atmost
// (nums, goal-1) to get the count of
// subarrays with sum at most goal
// and sum at most goal-1 respectively,
// then subtract them to get the count
// of subarrays with sum exactly
// equal to goal
return atmost(nums, goal) - atmost(nums, goal - 1);
}
// Driver code
int main()
{
vector<int> arr1 = { 1, 0, 1, 1, 0, 1 };
int k1 = 2;
// Function call
cout << numSubarraysWithSum(arr1, k1) << endl;
return 0;
}
Java
import java.util.*;
class Solution {
// Function to find count of subarrays with sum at most
// k
public int atmost(int[] nums, int k)
{
if (k < 0)
return 0;
int l = 0, cnt = 0;
int res = 0;
for (int i = 0; i < nums.length; i++) {
cnt += nums[i];
// Adjust the window sum by removing elements
// from the left until it is at most k
while (cnt > k && l<=i)
cnt -= nums[l++];
// Add the count of subarrays with sum at most k
// for the current window
res += (i - l + 1);
}
return res;
}
// Function to find count of subarrays with sum equal to
// k
public int numSubarraysWithSum(int[] nums, int goal)
{
// Call atmost(nums, goal) and atmost(nums, goal-1)
// to get the count of subarrays with sum at most
// goal and sum at most goal-1 respectively, then
// subtract them to get the count of subarrays with
// sum exactly equal to goal
return atmost(nums, goal) - atmost(nums, goal - 1);
}
public static void main(String[] args)
{
int[] arr1 = { 1, 0, 1, 1, 0, 1 };
int k1 = 2;
Solution solution = new Solution();
System.out.println(
"Count of subarrays with sum " + k1 + ": "
+ solution.numSubarraysWithSum(arr1, k1));
int[] arr2 = { 0, 0, 0, 0, 0 };
int k2 = 0;
System.out.println(
"Count of subarrays with sum " + k2 + ": "
+ solution.numSubarraysWithSum(arr2, k2));
}
}
Python
def atmost(nums, k):
if k < 0:
return 0
l = 0
cnt = 0
res = 0
for i in range(len(nums)):
cnt += nums[i]
# Adjust the window sum by removing
# elements from the left until it is
# at most k
while cnt > k and l<=i:
cnt -= nums[l]
l += 1
# Add the count of subarrays with
# sum at most k for the current window
res += (i - l + 1)
return res
def numSubarraysWithSum(nums, goal):
# Call atmost(nums, goal) and atmost
# (nums, goal-1) to get the count of
# subarrays with sum at most goal
# and sum at most goal-1 respectively,
# then subtract them to get the count
# of subarrays with sum exactly
# equal to goal
return atmost(nums, goal) - atmost(nums, goal - 1)
# Driver code
arr1 = [1, 0, 1, 1, 0, 1]
k1 = 2
# Function call
print(numSubarraysWithSum(arr1, k1))
## This code is contributed by codearcade
C#
using System;
public class Solution
{
// Function to find count of subarrays with sum at most
// k
public int Atmost(int[] nums, int k)
{
if (k < 0)
return 0;
int l = 0, cnt = 0;
int res = 0;
for (int i = 0; i < nums.Length; i++)
{
cnt += nums[i];
// Adjust the window sum by removing elements
// from the left until it is at most k
while (cnt > k && l<=i)
cnt -= nums[l++];
// Add the count of subarrays with sum at most k
// for the current window
res += (i - l + 1);
}
return res;
}
// Function to find count of subarrays with sum equal to
// k
public int NumSubarraysWithSum(int[] nums, int goal)
{
// Call Atmost(nums, goal) and Atmost(nums, goal-1)
// to get the count of subarrays with sum at most
// goal and sum at most goal-1 respectively, then
// subtract them to get the count of subarrays with
// sum exactly equal to goal
return Atmost(nums, goal) - Atmost(nums, goal - 1);
}
public static void Main(string[] args)
{
int[] arr1 = { 1, 0, 1, 1, 0, 1 };
int k1 = 2;
Solution solution = new Solution();
Console.WriteLine(
"Count of subarrays with sum " + k1 + ": "
+ solution.NumSubarraysWithSum(arr1, k1));
int[] arr2 = { 0, 0, 0, 0, 0 };
int k2 = 0;
Console.WriteLine(
"Count of subarrays with sum " + k2 + ": "
+ solution.NumSubarraysWithSum(arr2, k2));
}
}
Javascript
class Solution {
// Function to find count of subarrays with sum at most k
atmost(nums, k) {
if (k < 0) {
return 0;
}
let l = 0;
let cnt = 0;
let res = 0;
for (let i = 0; i < nums.length; i++) {
cnt += nums[i];
// Adjust the window sum by removing elements
// from the left until it is at most k
while (cnt > k && l<=i) {
cnt -= nums[l++];
}
// Add the count of subarrays with sum at most k
// for the current window
res += (i - l + 1);
}
return res;
}
// Function to find count of subarrays with sum equal to k
numSubarraysWithSum(nums, goal) {
// Call atmost(nums, goal) and atmost(nums, goal-1)
// to get the count of subarrays with sum at most
// goal and sum at most goal-1 respectively, then
// subtract them to get the count of subarrays with
// sum exactly equal to goal
return this.atmost(nums, goal) - this.atmost(nums, goal - 1);
}
}
const solution = new Solution();
const arr1 = [1, 0, 1, 1, 0, 1];
const k1 = 2;
console.log("Count of subarrays with sum " + k1 + ": " + solution.numSubarraysWithSum(arr1, k1));
const arr2 = [0, 0, 0, 0, 0];
const k2 = 0;
console.log("Count of subarrays with sum " + k2 + ": " + solution.numSubarraysWithSum(arr2, k2));
OutputCount of subarrays with sum 2: 6
Count of subarrays with sum 0: 15
Time Complexity: O(n)
Auxiliary Space:O(1)
Share your thoughts in the comments
Please Login to comment...