Generate a Binary String without any consecutive 0's and at most K consecutive 1's Last Updated : 13 Dec, 2022 Comments Improve Suggest changes Like Article Like Report Given two integers N and M, the task is to construct a binary string with the following conditions : The Binary String consists of N 0's and M 1'sThe Binary String has at most K consecutive 1's.The Binary String does not contain any adjacent 0's. If it is not possible to construct such a binary string, then print -1. Examples: Input: N = 5, M = 9, K = 2 Output: 01101101101101 Explanation: The string "01101101101101" satisfies the following conditions: No consecutive 0's are present.No more than K(= 2) consecutive 1's are present. Input: N = 4, M = 18, K = 4 Output: 1101111011110111101111 Approach: To construct a binary string satisfying the given properties, observe the following: For no two '0's to be consecutive, there should be at least a '1' placed between them.Therefore, for N number of '0's, there should be at least N-1 '1's present for a string of required type to be generated.Since no more than K consecutive '1's can be placed together, for N 0's, there can be a maximum (N+1) * K '1's possible.Therefore, the number of '1's should lie within the range: N - 1 ? M ? (N + 1) * K If the given values N and M do not satisfy the above condition, then print -1.Otherwise, follow the steps below to solve the problem:Append '0's to the final string.Insert '1' in between each pair of '0's. Subtract N - 1 from M, as N - 1 '1's have already been placed.For the remaining '1's, place min(K - 1, M) '1's alongside each already placed '1's, to ensure that no more than K '1's are placed together.For any remaining '1's, append them to the beginning and end of the final string.Finally, print the string generated. 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 construct the binary string string ConstructBinaryString(int N, int M, int K) { // Conditions when string construction // is not possible if (M < (N - 1) || M > K * (N + 1)) return "-1"; string ans = ""; // Stores maximum 1's that // can be placed in between int l = min(K, M / (N - 1)); int temp = N; while (temp--) { // Place 0's ans += '0'; if (temp == 0) break; // Place 1's in between for (int i = 0; i < l; i++) { ans += '1'; } } // Count remaining M's M -= (N - 1) * l; if (M == 0) return ans; l = min(M, K); // Place 1's at the end for (int i = 0; i < l; i++) ans += '1'; M -= l; // Place 1's at the beginning while (M > 0) { ans = '1' + ans; M--; } // Return the final string return ans; } // Driver Code int main() { int N = 5, M = 9, K = 2; cout << ConstructBinaryString(N, M, K); } Java // Java implementation of // the above approach import java.io.*; class GFG{ // Function to construct the binary string static String ConstructBinaryString(int N, int M, int K) { // Conditions when string construction // is not possible if (M < (N - 1) || M > K * (N + 1)) return "-1"; String ans = ""; // Stores maximum 1's that // can be placed in between int l = Math.min(K, M / (N - 1)); int temp = N; while (temp != 0) { temp--; // Place 0's ans += '0'; if (temp == 0) break; // Place 1's in between for(int i = 0; i < l; i++) { ans += '1'; } } // Count remaining M's M -= (N - 1) * l; if (M == 0) return ans; l = Math.min(M, K); // Place 1's at the end for(int i = 0; i < l; i++) ans += '1'; M -= l; // Place 1's at the beginning while (M > 0) { ans = '1' + ans; M--; } // Return the final string return ans; } // Driver code public static void main(String[] args) { int N = 5, M = 9, K = 2; System.out.println(ConstructBinaryString(N, M, K)); } } // This code is contributed by rutvik_56 Python3 # Python3 implementation of # the above approach # Function to construct the binary string def ConstructBinaryString(N, M, K): # Conditions when string construction # is not possible if(M < (N - 1) or M > K * (N + 1)): return '-1' ans = "" # Stores maximum 1's that # can be placed in between l = min(K, M // (N - 1)) temp = N while(temp): temp -= 1 # Place 0's ans += '0' if(temp == 0): break # Place 1's in between for i in range(l): ans += '1' # Count remaining M's M -= (N - 1) * l if(M == 0): return ans l = min(M, K) # Place 1's at the end for i in range(l): ans += '1' M -= l # Place 1's at the beginning while(M > 0): ans = '1' + ans M -= 1 # Return the final string return ans # Driver Code if __name__ == '__main__': N = 5 M = 9 K = 2 print(ConstructBinaryString(N, M , K)) # This code is contributed by Shivam Singh C# // C# implementation of // the above approach using System; class GFG{ // Function to construct the binary string static String ConstructBinaryString(int N, int M, int K) { // Conditions when string construction // is not possible if (M < (N - 1) || M > K * (N + 1)) return "-1"; string ans = ""; // Stores maximum 1's that // can be placed in between int l = Math.Min(K, M / (N - 1)); int temp = N; while (temp != 0) { temp--; // Place 0's ans += '0'; if (temp == 0) break; // Place 1's in between for(int i = 0; i < l; i++) { ans += '1'; } } // Count remaining M's M -= (N - 1) * l; if (M == 0) return ans; l = Math.Min(M, K); // Place 1's at the end for(int i = 0; i < l; i++) ans += '1'; M -= l; // Place 1's at the beginning while (M > 0) { ans = '1' + ans; M--; } // Return the final string return ans; } // Driver code public static void Main(string[] args) { int N = 5, M = 9, K = 2; Console.Write(ConstructBinaryString(N, M, K)); } } // This code is contributed by Ritik Bansal JavaScript <script> // JavaScript program for the above approach // Function to construct the binary string function ConstructBinaryString(N, M, K) { // Conditions when string construction // is not possible if (M < (N - 1) || M > K * (N + 1)) return "-1"; let ans = ""; // Stores maximum 1's that // can be placed in between let l = Math.min(K, M / (N - 1)); let temp = N; while (temp != 0) { temp--; // Place 0's ans += '0'; if (temp == 0) break; // Place 1's in between for(let i = 0; i < l; i++) { ans += '1'; } } // Count remaining M's M -= (N - 1) * l; if (M == 0) return ans; l = Math.min(M, K); // Place 1's at the end for(let i = 0; i < l; i++) ans += '1'; M -= l; // Place 1's at the beginning while (M > 0) { ans = '1' + ans; M--; } // Return the final string return ans; } // Driver Code let N = 5, M = 9, K = 2; document.write(ConstructBinaryString(N, M, K)); </script> Output: 01101101101101 Time Complexity: O(N+M)Auxiliary Space: O(N+M) Comment More infoAdvertise with us Next Article Generate a Binary String without any consecutive 0's and at most K consecutive 1's W webhead Follow Improve Article Tags : Strings Greedy Mathematical Combinatorial DSA misc Permutation and Combination +3 More Practice Tags : CombinatorialGreedyMathematicalMiscStrings +1 More Similar Reads Count of Binary strings having at most X consecutive 1s and Y consecutive 0s Given two integers N and M (1 ⤠N, M ⤠100) denoting the total number of 1s and 0s respectively. The task is to count the number of possible arrangements of these 0s and 1s such that any arrangement has at most X consecutive 1s and Y consecutive 0s (1 ⤠X, Y ⤠10). As the number of arrangements can 7 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 Generate all binary strings without consecutive 1's Given an integer n, the task is to generate all binary strings of size n without consecutive 1's.Examples: Input : n = 4Output : 0000 0001 0010 0100 0101 1000 1001 1010Input : n = 3Output : 000 001 010 100 101Approach:The idea is to generate all binary strings of length n without consecutive 1's usi 6 min read Count number of binary strings without consecutive 1âs : Set 2 Given a positive integer N, the task is to count all possible distinct binary strings of length N such that there are no consecutive 1âs. Examples: Input: N = 5 Output: 5 Explanation: The non-negative integers <= 5 with their corresponding binary representations are: 0 : 0 1 : 1 2 : 10 3 : 11 4 : 15+ min read Count number of binary strings without consecutive 1's Given a positive integer n, the task is to count all possible distinct binary strings of length n such that there are no consecutive 1's.Examples: Input: n = 3Output: 5Explanation: 5 strings are ("000", "001", "010", "100", "101").Input: n = 2Output: 3Explanation: 3 strings are ("00", "01", "10").Ta 15+ min read Count of binary strings of length N with even set bit count and at most K consecutive 1s Given two integers N and K, the task is to find the number of binary strings of length N having an even number of 1's out of which less than K are consecutive.Examples: Input: N = 4, K = 2 Output: 4 Explanation: The possible binary strings are 0000, 0101, 1001, 1010. They all have even number of 1's 12 min read Count of sub-strings with equal consecutive 0's and 1's Given binary string str of 0's and 1's only. The task is to count the total number of substrings of string str such that each substring has an equal number of consecutive 0's and 1's in it. Examples: Input: str = "010011" Output: 4 Explanation: The substrings with consecutive 0's and 1's are "01", " 9 min read Generate all Binary Strings of length N with equal count of 0s and 1s Given an integer N, the task is to generate all the binary strings with equal 0s and 1s. If no strings are possible, print -1 Examples: Input: N = 2 Output: â01â, â10âExplanation: All possible binary strings of length 2 are: 01, 10, 11, 00. Out of these, only 2 have equal number of 0s and 1s Input: 6 min read Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String Given a binary string S of size N, the task is to maximize the sum of the count of consecutive 0s present at the start and end of any of the rotations of the given string S. Examples: Input: S = "1001"Output: 2Explanation:All possible rotations of the string are:"1001": Count of 0s at the start = 0; 15+ min read Check if given Array can be made a binary Array with K consecutive 1s Given an array A[] of size N and an integer K. Each element of the array is either 0, 1 or 2. The task is to check if the array can be converted into a binary array (elements will be either 0 or 1) by replacing every element equal to 2 with either 0 or 1, such that there are only K occurrences of 1, 11 min read Like