# Print N-bit binary numbers having more 1’s than 0’s in all prefixes

Given a positive integer n, print all n-bit binary numbers having more 1’s than 0’s for any prefix of the number.

Examples:

```Input : n = 2
Output : 11 10

Input : n = 4
Output : 1111 1110 1101 1100 1011 1010
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A simple but not efficient solution will be to generate all N-bit binary numbers and print those numbers that satisfy the conditions. The time complexity of this solution is exponential.

An efficient solution is to generate only those N-bit numbers that satisfy the given conditions. We use recursion. At each point in the recursion, we append 0 and 1 to the partially formed number and recur with one less digit.

## C++

 `// CPP program to print all N-bit binary ` `#include ` `using` `namespace` `std; ` ` `  `/* function to generate n  digit numbers*/` `void` `printRec(string number, ``int` `extraOnes,  ` `                       ``int` `remainingPlaces) ` `{ ` `    ``/* if number generated */` `    ``if` `(0 == remainingPlaces) { ` `        ``cout << number << ``" "``; ` `        ``return``; ` `    ``} ` ` `  `    ``/* Append 1 at the current number and reduce  ` `       ``the remaining places by one */` `    ``printRec(number + ``"1"``, extraOnes + 1,  ` `                           ``remainingPlaces - 1); ` ` `  `    ``/* If more ones than zeros, append 0 to the  ` `       ``current number and reduce the remaining  ` `       ``places by one*/` `    ``if` `(0 < extraOnes)  ` `        ``printRec(number + ``"0"``, extraOnes - 1,  ` `                            ``remainingPlaces - 1);     ` `} ` ` `  `void` `printNums(``int` `n) ` `{ ` `    ``string str = ``""``; ` `    ``printRec(str, 0, n); ` `} ` ` `  `/*driver function*/` `int` `main() ` `{ ` `    ``int` `n = 4; ` `    ``printNums(n); ` `    ``return` `0; ` `} `

## Java

 `// java program to print all N-bit binary ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` `    ``// function to generate n digit numbers ` `    ``static` `void` `printRec(String number, ``int` `extraOnes,  ` `                                   ``int` `remainingPlaces) ` `    ``{ ` `        ``// if number generated  ` `        ``if` `(``0` `== remainingPlaces)  ` `        ``{ ` `            ``System.out.print( number +``" "``); ` `            ``return``; ` `        ``} ` `     `  `        ``// Append 1 at the current number and   ` `        ``// reduce the remaining places by one  ` `        ``printRec(number + ``"1"``, extraOnes + ``1``,  ` `                            ``remainingPlaces - ``1``); ` `     `  `        ``// If more ones than zeros, append 0 to the  ` `        ``// current number and reduce the remaining  ` `        ``// places by one ` `        ``if` `(``0` `< extraOnes)  ` `            ``printRec(number + ``"0"``, extraOnes - ``1``,  ` `                            ``remainingPlaces - ``1``);  ` `    ``} ` `     `  `    ``static` `void` `printNums(``int` `n) ` `    ``{ ` `        ``String str = ``""``; ` `        ``printRec(str, ``0``, n); ` `    ``} ` `     `  `    ``// Driver function ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{ ` `        ``int` `n = ``4``; ` `        ``printNums(n); ` `     `  `    ``} ` `} ` ` `  `// This code is contributed by vt_m `

## Python3

 `# Python 3 program to print all N-bit binary ` ` `  `# function to generate n digit numbers ` `def` `printRec(number, extraOnes, remainingPlaces): ` `     `  `    ``# if number generated  ` `    ``if` `(``0` `=``=` `remainingPlaces): ` `        ``print``(number, end ``=` `" "``) ` `        ``return` `     `  `    ``# Append 1 at the current number and  ` `    ``# reduce the remaining places by one  ` `    ``printRec(number ``+` `"1"``, extraOnes ``+` `1``,  ` `             ``remainingPlaces ``-` `1``) ` ` `  `    ``# If more ones than zeros, append 0 to  ` `    ``# the current number and reduce the  ` `    ``# remaining places by one ` `    ``if` `(``0` `< extraOnes): ` `        ``printRec(number ``+` `"0"``, extraOnes ``-` `1``,  ` `                         ``remainingPlaces ``-` `1``);  ` ` `  `def` `printNums(n): ` `    ``str` `=` `"" ` `    ``printRec(``str``, ``0``, n) ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``n ``=` `4` `    ``printNums(n) ` ` `  `# This code is contributed by ` `# Surendra_Gangwar `

## C#

 `// C# program to print all N-bit binary ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// function to generate n digit numbers ` `    ``static` `void` `printRec(String number,  ` `                         ``int` `extraOnes,  ` `                         ``int` `remainingPlaces) ` `    ``{ ` `         `  `        ``// if number generated  ` `        ``if` `(0 == remainingPlaces)  ` `        ``{ ` `            ``Console.Write( number +``" "``); ` `            ``return``; ` `        ``} ` `     `  `        ``// Append 1 at the current number and  ` `        ``// reduce the remaining places by one  ` `        ``printRec(number + ``"1"``, extraOnes + 1,  ` `                        ``remainingPlaces - 1); ` `     `  `        ``// If more ones than zeros, append  ` `        ``// 0 to the current number and  ` `        ``// reduce the remaining places  ` `        ``// by one ` `        ``if` `(0 < extraOnes)  ` `            ``printRec(number + ``"0"``, extraOnes - 1,  ` `                            ``remainingPlaces - 1);  ` `    ``} ` `    ``static` `void` `printNums(``int` `n) ` `    ``{ ` `        ``String str = ``""``; ` `        ``printRec(str, 0, n); ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main ()  ` `    ``{ ` `        ``int` `n = 4; ` `        ``printNums(n); ` `     `  `    ``} ` `} ` ` `  `// This code is contributed by Nitin Mittal. `

## PHP

 `

Output:

``` 1111 1110 1101 1100 1011 1010
```

This article is contributed by Pranav. 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.