Given two integers **N** and **M**, where **N** denotes the count of **‘0’ **and **M **denotes the count of **‘1’**, and an integer **K**, the task is to find the maximum number of binary strings that can be generated of the following two types:

- A string can consist of
**K**‘**0**‘s and a single ‘**1**‘. - A string can consist of
**K**‘**1**‘s and a single ‘**0**‘.

**Examples:**

Input:N = 4, M = 4, K = 2

Output:6

Explanation:

Count of ‘0‘s = 4

Count of ‘1‘s = 4

Possible ways to combine 0’s and 1’s under given constraints are {“001”, “001”} or {“001”, “110”} or {“110”, “110”}

Therefore, at most 2 combinations exists in a selection.

Each combination can be arranged inK + 1ways, i.e. “001” can be rearranged to form “010, “100” as well.

Therefore, the maximum possible strings that can be generated is 3 * 2 = 6

Input:N = 101, M = 231, K = 15

Output:320

**Approach:**

Follow the steps below to solve the problem:

- Consider the following three conditions to generate maximum possible combinations of binary strings:
- Number of combinations cannot exceed
**N**. - Number of combinations cannot exceed
**M**. - Number of combinations cannot exceed
**(A+B)/(K + 1)**.

- Number of combinations cannot exceed
- Therefore, the maximum possible combinations are
**min(A, B, (A + B)/ (K + 1))**. - Therefore, the maximum possible strings that can be generated are
**(K + 1) * min(A, B, (A + B)/ (K + 1))**. - Count pairs of strings that satisfy the given conditions
- 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 binary strings of length N having equal count of 0's and 1's
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Count of non-overlapping sub-strings "101" and "010" in the given binary string
- Count of binary strings of given length consisting of at least one 1
- Count number of binary strings without consecutive 1’s : Set 2
- Count binary strings with twice zeros in first half
- Count number of binary strings of length N having only 0's and 1's
- Count number of binary strings without consecutive 1's
- Count binary strings with k times appearing adjacent two set bits
- Count of distinct XORs formed by rearranging two Binary strings
- Count of Binary strings of length N having atmost M consecutive 1s or 0s alternatively exactly K times
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Queries to count distinct Binary Strings of all lengths from N to M satisfying given properties
- Number of strings in two array satisfy the given conditions
- Find the last remaining Character in the Binary String according to the given conditions
- Count of numbers in the range [L, R] which satisfy the given conditions
- Count of N digit numbers possible which satisfy the given conditions
- Count valid pairs in the array satisfying given conditions

Below is the implementation of the above approach:

## C++

`// C++ Program to implement ` `// the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to generate maximum ` `// possible strings that can be generated ` `long` `long` `countStrings(` `long` `long` `A, ` ` ` `long` `long` `B, ` ` ` `long` `long` `K) ` `{ ` ` ` ` ` `long` `long` `X = (A + B) / (K + 1); ` ` ` ` ` `// Maximum possible strings ` ` ` `return` `(min(A, min(B, X)) * (K + 1)); ` `} ` `int` `main() ` `{ ` ` ` ` ` `long` `long` `N = 101, M = 231, K = 15; ` ` ` `cout << countStrings(N, M, K); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `import` `java.io.*; ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to generate maximum ` `// possible strings that can be generated ` `static` `long` `countStrings(` `long` `A, ` `long` `B, ` ` ` `long` `K) ` `{ ` ` ` `long` `X = (A + B) / (K + ` `1` `); ` ` ` ` ` `// Maximum possible strings ` ` ` `return` `(Math.min(A, Math.min(B, X)) * ` ` ` `(K + ` `1` `)); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `long` `N = ` `101` `, M = ` `231` `, K = ` `15` `; ` ` ` ` ` `System.out.print(countStrings(N, M, K)); ` `} ` `} ` ` ` `// This code is contributed by offbeat ` |

*chevron_right*

*filter_none*

**Output:**

320

**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:

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.