Given two integers n and m where n is the number of 0s and m is the number of 1s. The task is to print all the 0s and 1s in a single row such that no two 0s are together and no three 1s are together. If it’s not possible to arrange 0s and 1s according to the condition then print -1.
Input: n = 1, m = 2
Input: n = 4, m = 8
Approach: We have answers only when, ( (n – 1) ≤ m and m ≤ 2 * (n + 1).
- If (m == n – 1) then print the pattern 010101… starting from 0 until all the 0s and 1s have been used.
- If (m > n) and m ≤ 2 * (n + 1) then print the pattern 110110110… until there is escessive 1s and change to pattern 0101010… when m becomes equal to n – 1.
Below is the implementation of the above approach:
- Program to print Spiral Pattern
- Print Fibonacci sequence using 2 variables
- Print N-bit binary numbers having more 1’s than 0’s in all prefixes
- Print a number as string of 'A' and 'B' in lexicographic order
- Given two binary strings perform operation until B > 0 and print the result
- Print reverse of a Linked List without extra space and modifications
- Print the longest prefix of the given string which is also the suffix of the same string
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Modulo of a large Binary String
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Sum of the digits of square of the given number which has only 1's as its digits
- Minimum given operations required to convert a given binary string to all 1's
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum number of Binary strings to represent a Number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.