Maximum count of distinct sized subarrays with given sum (original) (raw)
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++ `
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to find size of the largest // subset of subarrays having given sum // and size of each subarray is distinct int maxSubsetSize(int arr[], int N, int K) { // Stores starting index // of the current window int ptr = 0;
// Stores distinct subarray
// sizes of the subset
unordered_set<int> s;
// Stores the sum of
// current window
int curSum = 0;
// Loop to traverse over array
for (int i = 0; i < N; i++) {
// Update current window sum
curSum += arr[i];
// If sum is less that K
if (curSum < K) {
continue;
}
// If sum is more than K
if (curSum > K) {
// Decrease window size
while (curSum > K) {
curSum -= arr[ptr++];
}
}
if (curSum == K) {
// Insert size of the
// current window
s.insert(i - ptr + 1);
int t = ptr;
// Iterate over all windows
// with same sum
while (arr[t] == 0) {
s.insert(i - t);
t++;
}
}
}
// Return Answer
return s.size();
}
// Driver Code 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
// Java program for the above approach import java.util.HashSet;
class GFG {
// Function to find size of the largest // subset of subarrays having given sum // and size of each subarray is distinct static int maxSubsetSize(int arr[], int N, int K) { // Stores starting index // of the current window int ptr = 0;
// Stores distinct subarray
// sizes of the subset
HashSet<Integer> s = new HashSet<Integer>();
// Stores the sum of
// current window
int curSum = 0;
// Loop to traverse over array
for (int i = 0; i < N; i++) {
// Update current window sum
curSum += arr[i];
// If sum is less that K
if (curSum < K) {
continue;
}
// If sum is more than K
if (curSum > K) {
// Decrease window size
while (curSum > K) {
curSum -= arr[ptr++];
}
}
if (curSum == K) {
// Insert size of the
// current window
s.add(i - ptr + 1);
int t = ptr;
// Iterate over all windows
// with same sum
while (arr[t] == 0) {
s.add(i - t);
t++;
}
}
}
// Return Answer
return s.size();
}
// Driver Code 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));
} }
// This code is contributed by saurabh_jaiswal.
Python3
python3 program for the above approach
Function to find size of the largest
subset of subarrays having given sum
and size of each subarray is distinct
def maxSubsetSize(arr, N, K):
# Stores starting index
# of the current window
ptr = 0
# Stores distinct subarray
# sizes of the subset
s = set()
# Stores the sum of
# current window
curSum = 0
# Loop to traverse over array
for i in range(0, N):
# Update current window sum
curSum += arr[i]
# If sum is less that K
if (curSum < K):
continue
# If sum is more than K
if (curSum > K):
# Decrease window size
while (curSum > K):
curSum -= arr[ptr]
ptr += 1
if (curSum == K):
# Insert size of the
# current window
s.add(i - ptr + 1)
t = ptr
# Iterate over all windows
# with same sum
while (arr[t] == 0):
s.add(i - t)
t += 1
# Return Answer
return len(list(s))
Driver Code
if name == "main":
arr = [0, 1, 1, 0]
N = len(arr)
K = 2
print(maxSubsetSize(arr, N, K))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach using System; using System.Collections.Generic;
public class GFG {
// Function to find size of the largest // subset of subarrays having given sum // and size of each subarray is distinct static int maxSubsetSize(int []arr, int N, int K) {
// Stores starting index
// of the current window
int ptr = 0;
// Stores distinct subarray
// sizes of the subset
HashSet<int> s = new HashSet<int>();
// Stores the sum of
// current window
int curSum = 0;
// Loop to traverse over array
for (int i = 0; i < N; i++) {
// Update current window sum
curSum += arr[i];
// If sum is less that K
if (curSum < K) {
continue;
}
// If sum is more than K
if (curSum > K) {
// Decrease window size
while (curSum > K) {
curSum -= arr[ptr++];
}
}
if (curSum == K) {
// Insert size of the
// current window
s.Add(i - ptr + 1);
int t = ptr;
// Iterate over all windows
// with same sum
while (arr[t] == 0) {
s.Add(i - t);
t++;
}
}
}
// Return Answer
return s.Count;
}
// Driver Code 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));
} }
// This code is contributed by shikhasingrajput
JavaScript
`
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
- Count maximum non-overlapping subarrays with given sum Given an array arr[] consisting of N integers and an integer target, the task is to find the maximum number of non-empty non-overlapping subarrays such that the sum of array elements in each subarray is equal to the target. Examples: Input: arr[] = {2, -1, 4, 3, 6, 4, 5, 1}, target = 6Output: 3Expla 7 min read
- Maximum sum of K-length subarray with maximum count of distinct prime factors Given an array arr[] consisting of N positive integers and an integer K, the task is to find the maximum sum of array elements in a subarray having maximum sum of distinct prime factors in each K-length subarray. Note: If there are multiple answers then print the sum of the original subarray having 11 min read
- Size of The Subarray With Maximum Sum Given an array arr[] of size N, the task is to find the length of the subarray having maximum sum. Examples : Input : a[] = {1, -2, 1, 1, -2, 1} Output : Length of the subarray is 2 Explanation : Subarray with consecutive elements and maximum sum will be {1, 1}. So length is 2 Input : ar[] = { -2, - 10 min read
- Maximum size subset with given sum This is an extended version of the subset sum problem. Here we need to find the size of the maximum size subset whose sum is equal to the given sum. Examples: Input : set[] = {2, 3, 5, 7, 10, 15}, sum = 10 Output : 3 The largest sized subset with sum 10 is {2, 3, 5} Input : set[] = {1, 2, 3, 4, 5} s 8 min read
- Maximize the sum of maximum elements of at least K-sized subarrays Given an integer array arr[] of length N and an integer K, partition the array in some non-overlapping subarrays such that each subarray has size at least K and each element of the array should be part of a subarray. The task is to maximize the sum of maximum elements across all the subarrays. Examp 7 min read
- Count of subarrays with maximum value as K Given an array arr[] of N integers and an integer K. The task is to find the number of subarrays with a maximum value is equal to K. Examples: Input: arr[ ] = {2, 1, 3, 4}, K = 3Output: 3Explanation: Sub-arrays with maximum value is equals K are { 2, 1, 3 }, { 1, 3 }, { 3 }, hence the answer is 3. I 8 min read
- Maximum sum subarray of size K with sum less than X Given an array arr[] and two integers K and X, the task is to find the maximum sum among all subarrays of size K with the sum less than X. Examples: Input: arr[] = {20, 2, 3, 10, 5}, K = 3, X = 20Output: 18Explanation: Subarray of size 3 having maximum sum less than 20 is {3, 10, 5}. Therefore, requ 7 min read
- Maximize product of subarray sum with its maximum element Given an array arr[] consisting of N positive integers, the task is to find the maximum product of the subarray sum with the maximum element of that subarray. Examples: Input: arr[] = {2, -3, 8, -2, 5}Output: 88Explanation:The required maximum product can be obtained using subarray {8, -2, 5}Therefo 8 min read
- Count Subarrays With At Most K Distinct Elements Given an array arr[] and an integer k, the task is to find the count of subarrays such that each subarray has at most k distinct elements.Examples:Input: arr[] = [1, 2, 2, 3], k = 2 Output: 9Explanation: Subarrays with at most 2 distinct elements are: [1], [2], [2], [3], [1, 2], [2, 2], [2, 3], [1, 9 min read
- Maximum size subset with given sum using Backtracking Given an array arr[] consisting of N integers and an integer K, the task is to find the length of the longest subsequence with a sum equal to K.Examples: Input: arr[] = {-4, -2, -2, -1, 6}, K = 0 Output: 3 Explanation: The longest subsequence is of length 3 which is {-4, -2, 6} having sum 0.Input: a 9 min read