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’s
- The 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.
Input: N = 5, M = 9, K = 2
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
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:
Time Complexity: O(N+M)
Auxiliary Space: O(N+M)
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.
- Generate all binary strings without consecutive 1's
- Count possible binary strings of length N without P consecutive 0s and Q consecutive 1s
- Check if any permutation of a number without any leading zeros is a power of 2 or not
- Length of longest consecutive ones by at most one swap in a Binary String
- Strings formed from given characters without any consecutive repeating characters
- Minimum cost to generate any permutation of the given string
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Find the most frequent digit without using array/string
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Efficiently check if a string has all unique characters without using any additional data structure
- Reverse string without using any temporary variable
- Efficiently find first repeated character in a string without using any additional data structure in one traversal
- Convert string to integer without using any in-built functions
- Smallest string without any multiplication sign that represents the product of two given numbers
- Largest lexicographical string with at most K consecutive elements
- Lexicographically largest string possible consisting of at most K consecutive similar characters
- Count number of binary strings without consecutive 1’s : Set 2
- String matching with * (that matches with any) in any of the two strings
- Minimum distance between any most frequent and least frequent element of an array
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.