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

**Examples:**

Input:N = 5, M = 9, K = 2Output:01101101101101Explanation:

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 = 4Output: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.

- Append ‘
- 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);` `}` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of ` `// the above approach ` `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` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

**Output:**

01101101101101

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

## Recommended Posts:

- Generate all binary strings without consecutive 1's
- Check if a binary string contains consecutive same or not
- Length of longest consecutive ones by at most one swap in a Binary String
- Maximum Consecutive Zeroes in Concatenated Binary String
- Binary string with given frequencies of sums of consecutive pairs of characters
- Maximum length of consecutive 1's in a binary string in Python using Map function
- Generate original array from difference between every two consecutive elements
- Generate a list of n consecutive composite numbers (An interesting method)
- Fibbinary Numbers (No consecutive 1s in binary)
- 1 to n bit numbers with no consecutive 1s in binary representation.
- Count number of binary strings without consecutive 1’s : Set 2
- Find next greater element with no consecutive 1 in it's binary representation
- Find the number of binary strings of length N with at least 3 consecutive 1s
- Check if a string has m consecutive 1's or 0's
- Remove three consecutive duplicates from string
- Remove consecutive vowels from string
- Remove all consecutive duplicates from the string
- Consecutive sequenced numbers in a string
- Count of Binary strings of length N having atmost M consecutive 1s or 0s alternatively exactly K times
- Group consecutive characters of same type in a string

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.