Count of Binary Strings possible as per given conditions Last Updated : 17 Mar, 2021 Comments Improve Suggest changes Like Article Like Report 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 = 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).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 code N, M, K = 101, 231, 15 print(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 JavaScript <script> // JavaScript Program to implement // the above approach // Function to generate maximum // possible strings that can be generated function countStrings(A, B, K) { let X = Math.floor((A + B) / (K + 1)); // Maximum possible strings return (Math.min(A, Math.min(B, X)) * (K + 1)); } let N = 101, M = 231, K = 15; document.write(countStrings(N, M, K)); // This code is contributed by Surbhi Tyagi. </script> Output: 320 Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Count of Binary Strings possible as per given conditions S satyajitmahunta98 Follow Improve Article Tags : Strings Greedy Mathematical Combinatorial DSA binary-string Permutation and Combination +3 More Practice Tags : CombinatorialGreedyMathematicalStrings Similar Reads Count N-length Binary Strings consisting of "11" as substring Given a positive integer N, the task is to find the number of binary strings of length N which contains "11" as a substring. Examples: Input: N = 2Output: 1Explanation: The only string of length 2 that has "11" as a substring is "11". Input: N = 12Output: 3719 Approach: The idea is to derive the num 8 min read Count of substrings of a binary string containing K ones Given a binary string of length N and an integer K, we need to find out how many substrings of this string are exist which contains exactly K ones. Examples: Input : s = â10010â K = 1 Output : 9 The 9 substrings containing one 1 are, â1â, â10â, â100â, â001â, â01â, â1â, â10â, â0010â and â010âRecommen 7 min read Count of all possible N length balanced Binary Strings Given a number N, the task is to find the total number of balanced binary strings possible of length N. A binary string is said to be balanced if: The number of 0s and 1s are equal in each binary stringThe count of 0s in any prefix of binary strings is always greater than or equal to the count of 1s 6 min read Count of binary strings of given length consisting of at least one 1 Given an integer N, the task is to print the number of binary strings of length N which at least one '1'. Examples: Input: 2 Output: 3 Explanation: "01", "10" and "11" are the possible strings Input: 3 Output: 7 Explanation: "001", "011", "010", "100", "101", "110" and "111" are the possible strings 3 min read Count of distinct possible strings after performing given operations Given a numeric string S consisting of only three types of characters 0, 1, and 2 initially and following two operations: The occurrence of two consecutive 1 can be replaced by 3.The occurrence of two consecutive 2 can be replaced by 4.The given task is to find the total number of different strings 10 min read Count possible binary strings of length N without P consecutive 0s and Q consecutive 1s Given three integers N, P, and Q, the task is to count all possible distinct binary strings of length N such that each binary string does not contain P times consecutive 0âs and Q times consecutive 1's. Examples: Input: N = 5, P = 2, Q = 3Output: 7Explanation: Binary strings that satisfy the given c 15+ min read Count of groups of consecutive 1s in a given Binary String Given a binary string S of size N, the task is to find the number of groups of 1s only in the string S. Examples: Input: S = "100110111", N = 9Output: 3Explanation: The following groups are of 1s only: Group over the range [0, 0] which is equal to "1".Group over the range [3, 4] which is equal to "1 5 min read Count of Reverse Bitonic Substrings in a given String Given a string S, the task is to count the number of Reverse Bitonic Substrings in the given string. Reverse bitonic substring: A string in which the ASCII values of the characters of the string follow any of the following patterns: Strictly IncreasingStrictly decreasingDecreasing and then increasin 8 min read Count binary Strings that does not contain given String as Subsequence Given a number N and string S, count the number of ways to create a binary string (containing only '0' and '1') of size N such that it does not contain S as a subsequence. Examples: Input: N = 3, S = "10".Output: 4Explanation: There are 8 strings possible to fill 3 positions with 0's or 1's. {"000", 15+ min read Count permutations possible by replacing '?' characters in a Binary String Given a string S consisting of characters 0, 1, and '?', the task is to count all possible combinations of the binary string formed by replacing '?' by 0 or 1. Examples: Input: S = "0100?110"Output: 2Explanation: Replacing each '?'s with '1' and '0', the count of such strings formed will be equal to 3 min read Like