Length of longest subset consisting of A 0s and B 1s from an array of strings | Set 2
Last Updated :
26 Jul, 2025
Given an array arr[] consisting of N binary strings, and two integers A and B, the task is to find the length of the longest subset consisting of at most A 0s and B 1s.
Examples:
Input: arr[] = {“1”, “0”, “0001”, “10”, “111001”}, A = 5, B = 3
Output: 4
Explanation:
One possible way is to select the subset {arr[0], arr[1], arr[2], arr[3]}.
Total number of 0s and 1s in all these strings are 5 and 3 respectively.
Also, 4 is the length of the longest subset possible.
Input: arr[] = {“0”, “1”, “10”}, A = 1, B = 1
Output: 2
Explanation:
One possible way is to select the subset {arr[0], arr[1]}.
Total number of 0s and 1s in all these strings is 1 and 1 respectively.
Also, 2 is the length of the longest subset possible.
Naive Approach: Refer to the previous post of this article for the simplest approach to solve the problem.
Time Complexity: O(2N)
Auxiliary Space: O(1)
Dynamic Programming Approach: Refer to the previous post of this article for the Dynamic Programming approach.
Time Complexity: O(N*A*B)
Auxiliary Space: O(N * A * B)
Space-Optimized Dynamic Programming Approach: The space complexity in the above approach can be optimized based on the following observations:
- Initialize a 2D array, dp[A][B], where dp[i][j] represents the length of the longest subset consisting of at most i number of 0s and j number of 1s.
- To optimize the auxiliary space from the 3D table to the 2D table, the idea is to traverse the inner loops in reverse order.
- This ensures that whenever a value in dp[][] is changed, it will not be needed anymore in the current iteration.
- Therefore, the recurrence relation looks like this:
dp[i][j] = max (dp[i][j], dp[i - zeros][j - ones] + 1)
where zeros is the count of 0s and ones is the count of 1s in the current iteration.
Follow the steps below to solve the problem:
- Initialize a 2D array, say dp[A][B] and initialize all its entries as 0.
- Traverse the given array arr[] and for each binary string perform the following steps:
- After completing the above steps, print the value of dp[A][B] as the result.
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 the length of the
// longest subset of an array of strings
// with at most A 0s and B 1s
int MaxSubsetlength(vector<string> arr,
int A, int B)
{
// Initialize a 2D array with its
// entries as 0
int dp[A + 1][B + 1];
memset(dp, 0, sizeof(dp));
// Traverse the given array
for (auto& str : arr) {
// Store the count of 0s and 1s
// in the current string
int zeros = count(str.begin(),
str.end(), '0');
int ones = count(str.begin(),
str.end(), '1');
// Iterate in the range [A, zeros]
for (int i = A; i >= zeros; i--)
// Iterate in the range [B, ones]
for (int j = B; j >= ones; j--)
// Update the value of dp[i][j]
dp[i][j] = max(
dp[i][j],
dp[i - zeros][j - ones] + 1);
}
// Print the result
return dp[A][B];
}
// Driver Code
int main()
{
vector<string> arr
= { "1", "0", "0001",
"10", "111001" };
int A = 5, B = 3;
cout << MaxSubsetlength(arr, A, B);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Function to find the length of the
// longest subset of an array of strings
// with at most A 0s and B 1s
static int MaxSubsetlength(String arr[],
int A, int B)
{
// Initialize a 2D array with its
// entries as 0
int dp[][] = new int[A + 1][B + 1];
// Traverse the given array
for(String str : arr)
{
// Store the count of 0s and 1s
// in the current string
int zeros = 0, ones = 0;
for(char ch : str.toCharArray())
{
if (ch == '0')
zeros++;
else
ones++;
}
// Iterate in the range [A, zeros]
for(int i = A; i >= zeros; i--)
// Iterate in the range [B, ones]
for(int j = B; j >= ones; j--)
// Update the value of dp[i][j]
dp[i][j] = Math.max(
dp[i][j],
dp[i - zeros][j - ones] + 1);
}
// Print the result
return dp[A][B];
}
// Driver Code
public static void main(String[] args)
{
String arr[] = { "1", "0", "0001",
"10", "111001" };
int A = 5, B = 3;
System.out.println(MaxSubsetlength(arr, A, B));
}
}
// This code is contributed by Kingash
Python3
# Python3 program for the above approach
# Function to find the length of the
# longest subset of an array of strings
# with at most A 0s and B 1s
def MaxSubsetlength(arr, A, B):
# Initialize a 2D array with its
# entries as 0
dp = [[0 for i in range(B + 1)]
for i in range(A + 1)]
# Traverse the given array
for str in arr:
# Store the count of 0s and 1s
# in the current string
zeros = str.count('0')
ones = str.count('1')
# Iterate in the range [A, zeros]
for i in range(A, zeros - 1, -1):
# Iterate in the range [B, ones]
for j in range(B, ones - 1, -1):
# Update the value of dp[i][j]
dp[i][j] = max(dp[i][j],
dp[i - zeros][j - ones] + 1)
# Print the result
return dp[A][B]
# Driver Code
if __name__ == '__main__':
arr = [ "1", "0", "0001", "10", "111001" ]
A, B = 5, 3
print (MaxSubsetlength(arr, A, B))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG {
// Function to find the length of the
// longest subset of an array of strings
// with at most A 0s and B 1s
static int MaxSubsetlength(string[] arr, int A, int B)
{
// Initialize a 2D array with its
// entries as 0
int[, ] dp = new int[A + 1, B + 1];
// Traverse the given array
foreach(string str in arr)
{
// Store the count of 0s and 1s
// in the current string
int zeros = 0, ones = 0;
foreach(char ch in str.ToCharArray())
{
if (ch == '0')
zeros++;
else
ones++;
}
// Iterate in the range [A, zeros]
for (int i = A; i >= zeros; i--)
// Iterate in the range [B, ones]
for (int j = B; j >= ones; j--)
// Update the value of dp[i][j]
dp[i, j] = Math.Max(
dp[i, j],
dp[i - zeros, j - ones] + 1);
}
// Print the result
return dp[A, B];
}
// Driver Code
public static void Main(string[] args)
{
string[] arr = { "1", "0", "0001", "10", "111001" };
int A = 5, B = 3;
Console.WriteLine(MaxSubsetlength(arr, A, B));
}
}
// This code is contributed by ukasp.
JavaScript
<script>
// Javascript program for the above approach
// Function to find the length of the
// longest subset of an array of strings
// with at most A 0s and B 1s
function MaxSubsetlength(arr, A, B)
{
// Initialize a 2D array with its
// entries as 0
var dp = Array.from(Array(A + 1),
()=>Array(B + 1).fill(0));
// Traverse the given array
arr.forEach(str => {
// Store the count of 0s and 1s
// in the current string
var zeros = [...str].filter(x => x == '0').length;
var ones = [...str].filter(x => x == '1').length;
// Iterate in the range [A, zeros]
for(var i = A; i >= zeros; i--)
// Iterate in the range [B, ones]
for(var j = B; j >= ones; j--)
// Update the value of dp[i][j]
dp[i][j] = Math.max(dp[i][j],
dp[i - zeros][j - ones] + 1);
});
// Print the result
return dp[A][B];
}
// Driver Code
var arr = [ "1", "0", "0001",
"10", "111001" ];
var A = 5, B = 3;
document.write(MaxSubsetlength(arr, A, B));
// This code is contributed by noob2000
</script>
Time Complexity: O(N * A * B)
Auxiliary Space: O(A * B)
Efficient Approach : using array instead of 2d matrix to optimize space complexity
The optimization comes from the fact that the in this approach we use a 1D array, which requires less memory compared to a 2D array. However, in this approach code requires an additional condition to check if the number of zeros is less than or equal to the allowed limit.
Implementations Steps:
- Initialize an array dp of size B+1 with all entries as 0.
- Traverse through the given array of strings and for each string, count the number of 0's and 1's in it.
- For each string, iterate from B to the number of 1's in the string and update the value of dp[j] as maximum of dp[j] and dp[j - ones] + (1 if (j >= ones && A >= zeros) else 0).
- Finally, return dp[B] as the length of the longest subset of strings with at most A 0's and B 1's.
Implementation:
C++
// C++ program for above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the length of the
// longest subset of an array of strings
// with at most A 0s and B 1s
int MaxSubsetlength(vector<string> arr, int A, int B)
{
// Initialize a 1D array with its
// entries as 0
int dp[B + 1];
memset(dp, 0, sizeof(dp));
// Traverse the given array
for (auto& str : arr) {
// Store the count of 0s and 1s
// in the current string
int zeros = count(str.begin(), str.end(), '0');
int ones = count(str.begin(), str.end(), '1');
// Iterate in the range [B, ones]
for (int j = B; j >= ones; j--)
// Update the value of dp[j]
dp[j] = max(
dp[j],
dp[j - ones]
+ ((j >= ones && A >= zeros) ? 1 : 0));
}
// Print the result
return dp[B];
}
// Driver Code
int main()
{
vector<string> arr
= { "1", "0", "0001", "10", "111001" };
int A = 5, B = 3;
cout << MaxSubsetlength(arr, A, B);
return 0;
}
// this code is contributed by bhardwajji
Java
import java.util.*;
class Main {
// Function to find the length of the
// longest subset of an array of strings
// with at most A 0s and B 1s
static int MaxSubsetlength(List<String> arr, int A, int B) {
// Initialize a 1D array with its
// entries as 0
int[] dp = new int[B + 1];
Arrays.fill(dp, 0);
// Traverse the given array
for (String str : arr) {
// Store the count of 0s and 1s
// in the current string
int zeros = 0, ones = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '0') zeros++;
else ones++;
}
// Iterate in the range [B, ones]
for (int j = B; j >= ones; j--) {
// Update the value of dp[j]
dp[j] = Math.max(dp[j], dp[j - ones] + ((j >= ones && A >= zeros) ? 1 : 0));
}
}
// Print the result
return dp[B];
}
// Driver Code
public static void main(String[] args) {
List<String> arr = Arrays.asList("1", "0", "0001", "10", "111001");
int A = 5, B = 3;
System.out.println(MaxSubsetlength(arr, A, B));
}
}
Python3
# Python program for above approach
# Function to find the length of the
# longest subset of an array of strings
# with at most A 0s and B 1s
def MaxSubsetlength(arr, A, B):
# Initialize a 1D array with its
# entries as 0
dp = [0] * (B + 1)
# Traverse the given array
for str in arr:
# Store the count of 0s and 1s
# in the current string
zeros = str.count('0')
ones = str.count('1')
# Iterate in the range [B, ones]
for j in range(B, ones - 1, -1):
# Update the value of dp[j]
dp[j] = max(
dp[j],
dp[j - ones]
+ ((j >= ones and A >= zeros) == True))
# Print the result
return dp[B]
# Driver Code
arr = ["1", "0", "0001", "10", "111001"]
A, B = 5, 3
print(MaxSubsetlength(arr, A, B))
C#
// importing necessary namespaces
using System;
using System.Collections.Generic;
// defining main class
class MainClass
{
// Function to find the length of the longest subset
// of an array of strings with at most A 0s and B 1s
static int MaxSubsetlength(List<string> arr, int A, int B)
{
// Initialize a 1D array with its entries as 0
int[] dp = new int[B + 1];
Array.Fill(dp, 0);
// Traverse the given array
foreach (string str in arr)
{
// Store the count of 0s and 1s in the current string
int zeros = 0, ones = 0;
foreach (char c in str) {
if (c == '0') zeros++;
else ones++;
}
// Iterate in the range [B, ones]
for (int j = B; j >= ones; j--)
{
// Update the value of dp[j]
dp[j] = Math.Max(dp[j], dp[j - ones] + ((j >= ones && A >= zeros) ? 1 : 0));
}
}
// Return the result
return dp[B];
}
// Driver Code
public static void Main(string[] args)
{
// Define the input
List<string> arr = new List<string> {"1", "0", "0001", "10", "111001"};
int A = 5, B = 3;
// Call the function and print the result
Console.WriteLine(MaxSubsetlength(arr, A, B));
}
}
JavaScript
// Function to find the length of the
// longest subset of an array of strings
// with at most A 0s and B 1s
function MaxSubsetlength(arr, A, B) {
// Initialize a 1D array with its
// entries as 0
let dp = new Array(B + 1).fill(0);
// Traverse the given array
for (let i = 0; i < arr.length; i++) {
const str = arr[i];
// Store the count of 0s and 1s
// in the current string
let zeros = 0,
ones = 0;
for (let j = 0; j < str.length; j++) {
if (str.charAt(j) === '0') zeros++;
else ones++;
}
// Iterate in the range [B, ones]
for (let j = B; j >= ones; j--) {
// Update the value of dp[j]
dp[j] = Math.max(dp[j], dp[j - ones] + ((j >= ones && A >= zeros) ? 1 : 0));
}
}
// Print the result
return dp[B];
}
// Driver Code
const arr = ["1", "0", "0001", "10", "111001"];
const A = 5,
B = 3;
console.log(MaxSubsetlength(arr, A, B));
Output :
4
Time Complexity: O(N * A * B)
Auxiliary Space: O(B)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem