Maximum count of distinct sized subarrays with given sum
Last Updated :
11 Jan, 2022
Given a binary array arr[] of N integers, the task is to find the maximum count of distinct sized subarrays such that the sum of each subarray is K.
Example:
Input: arr[] = {0, 1, 1 , 0}, K = 2
Output: 3
Explanation: The subset {{0, 1, 1, 0}, {0, 1, 1}, {1, 1}} is the subset of 3 subarrays such that the sum of each subarray is 2 and the size of each subarray is distinct. The subarray {1, 1, 0} also has sum 2 but a subarray of size 3 is already included.
Input: arr[] = {0, 1, 0, 0, 0, 1 , 0}, K = 1
Output: 5
Approach: The given problem can be solved with the help of the sliding window algorithm. It can be observed that the sum of a subarray is equal to the count of 1’s in the subarray. Below are the steps to follow:
- Maintain a variable that keeps track of the count of 1’s in the current window.
- If the count of 1’s in the current window is less than K, increase the window size, and similarly if the count of 1’s is greater than K, decrease the window size.
- For windows with K sum, insert their size in a set data structure.
- The number of elements in the set after traversing the complete array is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxSubsetSize( int arr[], int N, int K)
{
int ptr = 0;
unordered_set< int > s;
int curSum = 0;
for ( int i = 0; i < N; i++) {
curSum += arr[i];
if (curSum < K) {
continue ;
}
if (curSum > K) {
while (curSum > K) {
curSum -= arr[ptr++];
}
}
if (curSum == K) {
s.insert(i - ptr + 1);
int t = ptr;
while (arr[t] == 0) {
s.insert(i - t);
t++;
}
}
}
return s.size();
}
int main()
{
int arr[] = { 0, 1, 1, 0 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 2;
cout << maxSubsetSize(arr, N, K);
return 0;
}
|
Java
import java.util.HashSet;
class GFG {
static int maxSubsetSize( int arr[], int N, int K) {
int ptr = 0 ;
HashSet<Integer> s = new HashSet<Integer>();
int curSum = 0 ;
for ( int i = 0 ; i < N; i++) {
curSum += arr[i];
if (curSum < K) {
continue ;
}
if (curSum > K) {
while (curSum > K) {
curSum -= arr[ptr++];
}
}
if (curSum == K) {
s.add(i - ptr + 1 );
int t = ptr;
while (arr[t] == 0 ) {
s.add(i - t);
t++;
}
}
}
return s.size();
}
public static void main(String args[]) {
int arr[] = { 0 , 1 , 1 , 0 };
int N = arr.length;
int K = 2 ;
System.out.println(maxSubsetSize(arr, N, K));
}
}
|
Python3
def maxSubsetSize(arr, N, K):
ptr = 0
s = set ()
curSum = 0
for i in range ( 0 , N):
curSum + = arr[i]
if (curSum < K):
continue
if (curSum > K):
while (curSum > K):
curSum - = arr[ptr]
ptr + = 1
if (curSum = = K):
s.add(i - ptr + 1 )
t = ptr
while (arr[t] = = 0 ):
s.add(i - t)
t + = 1
return len ( list (s))
if __name__ = = "__main__" :
arr = [ 0 , 1 , 1 , 0 ]
N = len (arr)
K = 2
print (maxSubsetSize(arr, N, K))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static int maxSubsetSize( int []arr, int N, int K)
{
int ptr = 0;
HashSet< int > s = new HashSet< int >();
int curSum = 0;
for ( int i = 0; i < N; i++) {
curSum += arr[i];
if (curSum < K) {
continue ;
}
if (curSum > K) {
while (curSum > K) {
curSum -= arr[ptr++];
}
}
if (curSum == K) {
s.Add(i - ptr + 1);
int t = ptr;
while (arr[t] == 0) {
s.Add(i - t);
t++;
}
}
}
return s.Count;
}
public static void Main(String []args) {
int []arr = { 0, 1, 1, 0 };
int N = arr.Length;
int K = 2;
Console.WriteLine(maxSubsetSize(arr, N, K));
}
}
|
Javascript
<script>
function maxSubsetSize(arr, N, K) {
let ptr = 0;
let s = new Set();
let curSum = 0;
for (let i = 0; i < N; i++) {
curSum += arr[i];
if (curSum < K) {
continue ;
}
if (curSum > K) {
while (curSum > K) {
curSum -= arr[ptr++];
}
}
if (curSum == K) {
s.add(i - ptr + 1);
let t = ptr;
while (arr[t] == 0) {
s.add(i - t);
t++;
}
}
}
return s.size;
}
let arr = [0, 1, 1, 0];
let N = arr.length;
let K = 2;
document.write(maxSubsetSize(arr, N, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...