Print n 0 s and m 1 s such that no two 0 s and no three 1 s are together | C ++ implementation of the approach ; Function to print the required pattern ; When condition fails ; When m = n - 1 ; Driver program
#include <bits/stdc++.h> NEW_LINE 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 ; }