Maximize count of non-overlapping subarrays with sum K
Last Updated :
11 Jul, 2022
Given an array arr[] and an integer K, the task is to print the maximum number of non-overlapping subarrays with a sum equal to K.
Examples:
Input: arr[] = {-2, 6, 6, 3, 5, 4, 1, 2, 8}, K = 10
Output: 3
Explanation: All possible non-overlapping subarrays with sum K(= 10) are {-2, 6, 6}, {5, 4, 1}, {2, 8}. Therefore, the required count is 3.
Input: arr[] = {1, 1, 1}, K = 2
Output: 1
Approach: The problem can be solved using the concept of prefix sum. Follow the below steps to solve the problem:
- Initialize a set to store all the prefix sums obtained up to the current element.
- Initialize variables prefixSum and res, to store the prefix sum of the current subarray and the count of subarrays with a sum equal to K respectively.
- Iterate over the array and for each array element, update prefixSum by adding to it the current element. Now, check if the value prefixSum – K is already present in the set or not. If found to be true, increment res, clear the set, and reset the value of prefixSum.
- Repeat the above steps until the entire array is traversed. Finally, print the value of res.
C++14
#include <bits/stdc++.h>
using namespace std;
int CtSubarr( int arr[], int N, int K)
{
unordered_set< int > st;
int prefixSum = 0;
st.insert(prefixSum);
int res = 0;
for ( int i = 0; i < N; i++) {
prefixSum += arr[i];
if (st.count(prefixSum - K)) {
res += 1;
prefixSum = 0;
st.clear();
st.insert(0);
}
st.insert(prefixSum);
}
return res;
}
int main()
{
int arr[] = { -2, 6, 6, 3, 5, 4, 1, 2, 8 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 10;
cout << CtSubarr(arr, N, K);
}
|
Java
import java.util.*;
class GFG{
static int CtSubarr( int [] arr,
int N, int K)
{
Set<Integer> st = new HashSet<Integer>();
int prefixSum = 0 ;
st.add(prefixSum);
int res = 0 ;
for ( int i = 0 ; i < N; i++)
{
prefixSum += arr[i];
if (st.contains(prefixSum - K))
{
res += 1 ;
prefixSum = 0 ;
st.clear();
st.add( 0 );
}
st.add(prefixSum);
}
return res;
}
public static void main(String[] args)
{
int arr[] = {- 2 , 6 , 6 , 3 ,
5 , 4 , 1 , 2 , 8 };
int N = arr.length;
int K = 10 ;
System.out.println(CtSubarr(arr, N, K));
}
}
|
Python3
def CtSubarr(arr, N, K):
st = set ()
prefixSum = 0
st.add(prefixSum)
res = 0
for i in range (N):
prefixSum + = arr[i]
if ((prefixSum - K) in st):
res + = 1
prefixSum = 0
st.clear()
st.add( 0 )
st.add(prefixSum)
return res
arr = [ - 2 , 6 , 6 , 3 , 5 , 4 , 1 , 2 , 8 ]
N = len (arr)
K = 10
print (CtSubarr(arr, N, K))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int CtSubarr( int [] arr,
int N, int K)
{
HashSet< int > st = new HashSet< int >();
int prefixSum = 0;
st.Add(prefixSum);
int res = 0;
for ( int i = 0; i < N; i++)
{
prefixSum += arr[i];
if (st.Contains(prefixSum - K))
{
res += 1;
prefixSum = 0;
st.Clear();
st.Add(0);
}
st.Add(prefixSum);
}
return res;
}
public static void Main(String[] args)
{
int []arr = { -2, 6, 6, 3,
5, 4, 1, 2, 8};
int N = arr.Length;
int K = 10;
Console.WriteLine(CtSubarr(arr, N, K));
}
}
|
Javascript
<script>
function CtSubarr(arr, N, K)
{
var st = new Set();
var prefixSum = 0;
st.add(prefixSum);
var res = 0;
for ( var i = 0; i < N; i++) {
prefixSum += arr[i];
if (st.has(prefixSum - K)) {
res += 1;
prefixSum = 0;
st = new Set();
st.add(0);
}
st.add(prefixSum);
}
return res;
}
var arr = [-2, 6, 6, 3, 5, 4, 1, 2, 8];
var N = arr.length;
var K = 10;
document.write( CtSubarr(arr, N, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Share your thoughts in the comments
Please Login to comment...