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 in K + 1 ways, i.e. “001” can be rearranged to form “010, “100” as well.
Therefore, the maximum possible strings that can be generated is 3 * 2 = 6Input: 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).
- 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)).
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; } |
Java
// Java program to implement // 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 |
Python3
# Python3 program to implement # the above approach # Function to generate maximum # possible strings that can be # generated def countStrings(A, B, K): X = (A + B) // (K + 1) # Maximum possible strings return (min(A, min(B, X)) * (K + 1)) # Driver codeN, M, K = 101, 231, 15print(countStrings(N, M, K))# This code is contributed divyeshrabadiya07 |
C#
// C# program to implement // the above approach using System; 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; Console.Write(countStrings(N, M, K)); } } // This code is contributed by rock_cool |
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:
- Maximum modified Array sum possible by choosing elements as per the given conditions
- Maximum number of objects that can be created as per given conditions
- Minimum cost to reduce the integer N to 1 as per given conditions
- Maximize the last Array element as per the given conditions
- Count possible permutations of given array satisfying the given conditions
- Is it possible to reach N and M from 1 and 0 respectively as per given conditio
- Count of N digit numbers possible which satisfy the given conditions
- Count pairs of strings that satisfy the given conditions
- Generate a String from given Strings P and Q based on 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
- Maximum Sum possible by selecting X elements from a Matrix based on given conditions
- Number of strings in two array satisfy the given conditions
- Generate a string from an array of alphanumeric strings based on given conditions
- Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array
- Maximize Profit by trading stocks based on given rate per day
- Arrange the numbers in the Array as per given inequalities
- Nth angle of a Polygon whose initial angle and per angle increment is given
- Perform range sum queries on string as per given condition
- Find the last remaining Character in the Binary String according to the given conditions
- Count valid pairs in the array satisfying given conditions
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.

