Given two numbers **N** and **M** which denotes the count of ones and zeros respectively, the task is to maximize the count of binary strings of length 3, consisting of both 0 and 1 in them, that can be formed from the given **N 1s** and **M 0s**.**Examples:**

Input:N = 4, M = 5Output:3Explanation:

Possible strings = { “001”, “011”, “001” }Input:N = 818, M = 234Output:234

**Naive Approach:** Binary strings of three lengths can be formed as per the below conditions:

**If N > M:**If N > 2, then reduce N by 2, M by 1, and since a string of type**110**is generated, thus increase the count by 1.**If N ≤ M:**If M > 2, then reduce M by 2, N by 1, and since a string of type**001**is generated, thus increase the count by 1.

Therefore, the idea is to iterate a loop until N or M becomes zero and keep updating the count of the string according to the above conditions.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function that counts the number of` `// strings of length three that can be` `// made with given m 0s and n 1s` `void` `number_of_strings(` `int` `N, ` `int` `M)` `{` ` ` `int` `ans = 0;` ` ` `// Iterate until N & M are positive` ` ` `while` `(N > 0 && M > 0) {` ` ` `// Case 1:` ` ` `if` `(N > M) {` ` ` `if` `(N >= 2) {` ` ` `N -= 2;` ` ` `--M;` ` ` `++ans;` ` ` `}` ` ` `else` `{` ` ` `break` `;` ` ` `}` ` ` `}` ` ` `// Case 2:` ` ` `else` `{` ` ` `if` `(M >= 2) {` ` ` `M -= 2;` ` ` `--N;` ` ` `++ans;` ` ` `}` ` ` `else` `{` ` ` `break` `;` ` ` `}` ` ` `}` ` ` `}` ` ` `// Print the count of strings` ` ` `cout << ans;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given count of 1s and 0s` ` ` `int` `N = 4, M = 19;` ` ` `// Function Call` ` ` `number_of_strings(N, M);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach` `import` `java.util.*;` `import` `java.lang.*;` `class` `GFG{` ` ` `// Function that counts the number of` `// strings of length three that can be` `// made with given m 0s and n 1s` `static` `void` `number_of_strings(` `int` `N, ` `int` `M)` `{` ` ` `int` `ans = ` `0` `;` ` ` `// Iterate until N & M are positive` ` ` `while` `(N > ` `0` `&& M > ` `0` `) ` ` ` `{` ` ` ` ` `// Case 1:` ` ` `if` `(N > M) ` ` ` `{` ` ` `if` `(N >= ` `2` `) ` ` ` `{` ` ` `N -= ` `2` `;` ` ` `--M;` ` ` `++ans;` ` ` `}` ` ` `else` ` ` `{` ` ` `break` `;` ` ` `}` ` ` `}` ` ` ` ` `// Case 2:` ` ` `else` ` ` `{` ` ` `if` `(M >= ` `2` `) ` ` ` `{` ` ` `M -= ` `2` `;` ` ` `--N;` ` ` `++ans;` ` ` `}` ` ` `else` ` ` `{` ` ` `break` `;` ` ` `}` ` ` `}` ` ` `}` ` ` ` ` `// Print the count of strings` ` ` `System.out.println(ans);` `}` `// Driver Code` `public` `static` `void` `main (String[] args)` `{` ` ` ` ` `// Given count of 1s and 0s` ` ` `int` `N = ` `4` `, M = ` `19` `;` ` ` `// Function call` ` ` `number_of_strings(N, M);` `}` `}` `// This code is contributed by jana_sayantan` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach` `# Function that counts the number of` `# strings of length three that can be` `# made with given m 0s and n 1s` `def` `number_of_strings(N, M):` ` ` `ans ` `=` `0` ` ` `# Iterate until N & M are positive` ` ` `while` `(N > ` `0` `and` `M > ` `0` `):` ` ` `# Case 1:` ` ` `if` `(N > M):` ` ` `if` `(N >` `=` `2` `):` ` ` `N ` `-` `=` `2` ` ` `M ` `-` `=` `1` ` ` `ans ` `+` `=` `1` ` ` ` ` `else` `:` ` ` `break` ` ` ` ` `# Case 2:` ` ` `else` `:` ` ` `if` `M >` `=` `2` `:` ` ` `M ` `-` `=` `2` ` ` `N ` `-` `=` `1` ` ` `ans ` `+` `=` `1` ` ` ` ` `else` `:` ` ` `break` ` ` ` ` `# Print the count of strings` ` ` `print` `(ans)` `# Driver code` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `# Given count of 1s and 0s` ` ` `N ` `=` `4` ` ` `M ` `=` `19` ` ` `# Function call` ` ` `number_of_strings(N, M)` `# This code is contributed by jana_sayantan` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` ` ` `// Function that counts the number of` `// strings of length three that can be` `// made with given m 0s and n 1s` `static` `void` `number_of_strings(` `int` `N, ` `int` `M)` `{` ` ` `int` `ans = 0;` ` ` `// Iterate until N & M are positive` ` ` `while` `(N > 0 && M > 0)` ` ` `{` ` ` ` ` `// Case 1:` ` ` `if` `(N > M)` ` ` `{` ` ` `if` `(N >= 2)` ` ` `{` ` ` `N -= 2;` ` ` `--M;` ` ` `++ans;` ` ` `}` ` ` `else` ` ` `{` ` ` `break` `;` ` ` `}` ` ` `}` ` ` `// Case 2:` ` ` `else` ` ` `{` ` ` `if` `(M >= 2)` ` ` `{` ` ` `M -= 2;` ` ` `--N;` ` ` `++ans;` ` ` `}` ` ` `else` ` ` `{` ` ` `break` `;` ` ` `}` ` ` `}` ` ` `}` ` ` ` ` `// Print the count of strings` ` ` `Console.WriteLine(ans);` `}` `// Driver Code` `public` `static` `void` `Main (String[] args)` `{` ` ` ` ` `// Given count of 1s and 0s` ` ` `int` `N = 4, M = 19;` ` ` `// Function call` ` ` `number_of_strings(N, M);` `}` `}` `// This code is contributed by jana_sayantan` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(max(A, B)) **Auxiliary Space:** O(1)**Efficient Approach:** To optimize the above approach, observe that the total number of binary strings that can be formed will be a minimum of N, M, and (N + M)/3 as:

- If N is minimum, and we have M ≥ 2*N then all the string of type
**110**can be made. - If M is minimum, and we have N ≥ 2*M then all the string of type
**001**can be made. - Else the total count will be (N + M)/3 and strings of both types
**110**and**001**can be made.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function that counts the number of` `// strings of length 3 that can be` `// made with given m 0s and n 1s` `void` `number_of_strings(` `int` `N, ` `int` `M)` `{` ` ` `// Print the count of strings` ` ` `cout << min(N, min(M, (N + M) / 3));` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given count of 1s and 0s` ` ` `int` `N = 4, M = 19;` ` ` `// Function Call` ` ` `number_of_strings(N, M);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for ` `// the above approach` `class` `GFG{` `// Function that counts the number of` `// Strings of length 3 that can be` `// made with given m 0s and n 1s` `static` `void` `number_of_Strings(` `int` `N, ` `int` `M)` `{` ` ` `// Print the count of Strings` ` ` `System.out.print(Math.min(N, ` ` ` `Math.min(M, ` ` ` `(N + M) / ` ` ` `3` `)));` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `// Given count of 1s and 0s` ` ` `int` `N = ` `4` `, M = ` `19` `;` ` ` `// Function Call` ` ` `number_of_Strings(N, M);` `}` `}` `// This code is contributed by shikhasingrajput` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach` `# Function that counts the number of` `# strings of length 3 that can be` `# made with given m 0s and n 1s` `def` `number_of_strings(N, M):` ` ` `# Print the count of strings` ` ` `print` `(` `min` `(N, ` `min` `(M, (N ` `+` `M) ` `/` `/` `3` `)))` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `# Given count of 1s and 0s` ` ` `N ` `=` `4` ` ` `M ` `=` `19` ` ` `# Function call` ` ` `number_of_strings(N, M)` `# This code is contributed by mohit kumar 29` |

*chevron_right*

*filter_none*

## C#

`// C# program for ` `// the above approach` `using` `System;` `class` `GFG{` `// Function that counts the number of` `// Strings of length 3 that can be` `// made with given m 0s and n 1s` `static` `void` `number_of_Strings(` `int` `N, ` ` ` `int` `M)` `{` ` ` `// Print the count of Strings` ` ` `Console.Write(Math.Min(N, ` ` ` `Math.Min(M, (N + M) / 3)));` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `// Given count of 1s and 0s` ` ` `int` `N = 4, M = 19;` ` ` `// Function Call` ` ` `number_of_Strings(N, M);` `}` `}` `// This code is contributed by shikhasingrajput` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(1) **Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- All possible strings of any length that can be formed from a given string
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count of strings that can be formed using a, b and c under given constraints
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Count of strings that can be formed from another string using each character at-most once
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
- Maximum number of strings that can be formed with given zeros and ones
- Longest palindrome formed by concatenating and reordering strings of equal length
- Check if given string can be formed by two other strings or their permutations
- Find the numbers of strings that can be formed after processing Q queries
- Check if K palindromic strings can be formed from a given string
- Check if a Palindromic String can be formed by concatenating Substrings of two given Strings
- K length words that can be formed from given characters without repetition
- Count of same length Strings that exists lexicographically in between two given Strings
- Maximize length of the String by concatenating characters from an Array of Strings
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Count of binary strings of length N having equal count of 0's and 1's
- Count of distinct XORs formed by rearranging two Binary strings
- Count of alphabets whose ASCII values can be formed with the digits of N

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.