Print n 0s and m 1s such that no two 0s and no three 1s are together
Last Updated :
20 Feb, 2023
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.
Examples:
Input: n = 1, m = 2
Output: 011
Input: n = 4, m = 8
Output: 110110110101
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 excessive 1s and change to pattern 0101010… when m becomes equal to n – 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printPattern( int n, int m)
{
if (m > 2 * (n + 1) || m < n - 1) {
cout << "-1" ;
}
else if ( abs (n - m) <= 1) {
while (n > 0 && m > 0) {
cout << "01" ;
n--;
m--;
}
if (n != 0) {
cout << "0" ;
}
if (m != 0) {
cout << "1" ;
}
}
else {
while (m - n > 1 && n > 0) {
cout << "110" ;
m = m - 2;
n = n - 1;
}
while (n > 0) {
cout << "10" ;
n--;
m--;
}
while (m > 0) {
cout << "1" ;
m--;
}
}
}
int main()
{
int n = 4, m = 8;
printPattern(n, m);
return 0;
}
|
Java
class GFG
{
static void printPattern( int n, int m)
{
if (m > 2 * (n + 1 ) || m < n - 1 )
{
System.out.print( "-1" );
}
else if (Math.abs(n - m) <= 1 )
{
while (n > 0 && m > 0 )
{
System.out.print( "01" );
n--;
m--;
}
if (n != 0 )
{
System.out.print( "0" );
}
if (m != 0 )
{
System.out.print( "1" );
}
}
else
{
while (m - n > 1 && n > 0 )
{
System.out.print( "110" );
m = m - 2 ;
n = n - 1 ;
}
while (n > 0 )
{
System.out.print( "10" );
n--;
m--;
}
while (m > 0 )
{
System.out.print( "1" );
m--;
}
}
}
public static void main(String []args)
{
int n = 4 , m = 8 ;
printPattern(n, m);
}
}
|
Python3
def printPattern(n, m):
if (m > 2 * (n + 1 ) or m < n - 1 ):
print ( "-1" , end = "")
elif ( abs (n - m) < = 1 ):
while (n > 0 and m > 0 ):
print ( "01" , end = "");
n - = 1
m - = 1
if (n ! = 0 ):
print ( "0" , end = "")
if (m ! = 0 ):
print ( "1" , end = "")
else :
while (m - n > 1 and n > 0 ):
print ( "110" , end = "")
m = m - 2
n = n - 1
while (n > 0 ):
print ( "10" , end = "")
n - = 1
m - = 1
while (m > 0 ):
print ( "1" , end = "")
m - = 1
if __name__ = = '__main__' :
n = 4
m = 8
printPattern(n, m)
|
C#
using System;
class GFG
{
static void printPattern( int n, int m)
{
if (m > 2 * (n + 1) || m < n - 1)
{
Console.Write( "-1" );
}
else if (Math.Abs(n - m) <= 1)
{
while (n > 0 && m > 0)
{
Console.Write( "01" );
n--;
m--;
}
if (n != 0)
{
Console.Write( "0" );
}
if (m != 0)
{
Console.Write( "1" );
}
}
else
{
while (m - n > 1 && n > 0)
{
Console.Write( "110" );
m = m - 2;
n = n - 1;
}
while (n > 0)
{
Console.Write( "10" );
n--;
m--;
}
while (m > 0)
{
Console.Write( "1" );
m--;
}
}
}
public static void Main()
{
int n = 4, m = 8;
printPattern(n, m);
}
}
|
PHP
<?php
function printPattern( $n , $m )
{
if ( $m > 2 * ( $n + 1) || $m < $n - 1)
{
echo ( "-1" );
}
else if ( abs ( $n - $m ) <= 1)
{
while ( $n > 0 && $m > 0)
{
System.out. print ( "01" );
$n --;
$m --;
}
if ( $n != 0)
{
echo ( "0" );
}
if ( $m != 0)
{
echo ( "1" );
}
}
else
{
while ( $m - $n > 1 && $n > 0)
{
echo ( "110" );
$m = $m - 2;
$n = $n - 1;
}
while ( $n > 0)
{
echo ( "10" );
$n --;
$m --;
}
while ( $m > 0)
{
echo ( "1" );
$m --;
}
}
}
$n = 4; $m = 8;
printPattern( $n , $m );
?>
|
Javascript
<script>
function printPattern( n, m)
{
if (m > 2 * (n + 1) || m < n - 1) {
document.write( "-1" );
}
else if (Math.abs(n - m) <= 1) {
while (n > 0 && m > 0) {
document.write( "01" );
n--;
m--;
}
if (n != 0) {
document.write( "0" );
}
if (m != 0) {
document.write( "1" );
}
}
else {
while (m - n > 1 && n > 0) {
document.write( "110" );
m = m - 2;
n = n - 1;
}
while (n > 0) {
document.write( "10" );
n--;
m--;
}
while (m > 0) {
document.write( "1" );
m--;
}
}
}
var n = 4, m = 8;
printPattern(n, m);
</script>
|
Time complexity : O(m)
Space complexity : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...