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** **0**s and** B** **1**s.

**Examples:**

Input:arr[] = {“1”, “0”, “0001”, “10”, “111001”}, A = 5, B = 3Output:4Explanation: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 = 1Output:2Explanation: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(2^{N})**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**0**s and**j**number of**1**s. - 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:- Store the count of 0s and 1s in the current string in variables
**zeros**and**ones**respectively. - Iterate in the range
**[A, zeros]**using the variable**i**and simultaneously iterate in the range**[B, ones]**using the variable**j**and update the value of**dp[i][j]**to maximum of**dp[i][j]**and**(dp[i – zeros][j – ones] + 1)**.

- Store the count of 0s and 1s in the current string in variables
- 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.` |

**Output:**

4

**Time Complexity:** O(N * A * B)**Auxiliary Space:** O(A * B)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**