Print N-bit binary numbers having more 1’s than 0’s in all prefixes
Last Updated :
19 Jul, 2022
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
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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void printRec(string number, int extraOnes,
int remainingPlaces)
{
if (0 == remainingPlaces) {
cout << number << " " ;
return ;
}
printRec(number + "1" , extraOnes + 1,
remainingPlaces - 1);
if (0 < extraOnes)
printRec(number + "0" , extraOnes - 1,
remainingPlaces - 1);
}
void printNums( int n)
{
string str = "" ;
printRec(str, 0, n);
}
int main()
{
int n = 4;
printNums(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void printRec(String number,
int extraOnes,
int remainingPlaces)
{
if ( 0 == remainingPlaces) {
System.out.print(number + " " );
return ;
}
printRec(number + "1" , extraOnes + 1 ,
remainingPlaces - 1 );
if ( 0 < extraOnes)
printRec(number + "0" , extraOnes - 1 ,
remainingPlaces - 1 );
}
static void printNums( int n)
{
String str = "" ;
printRec(str, 0 , n);
}
public static void main(String[] args)
{
int n = 4 ;
printNums(n);
}
}
|
Python3
def printRec(number, extraOnes, remainingPlaces):
if ( 0 = = remainingPlaces):
print (number, end = " " )
return
printRec(number + "1" , extraOnes + 1 ,
remainingPlaces - 1 )
if ( 0 < extraOnes):
printRec(number + "0" , extraOnes - 1 ,
remainingPlaces - 1 )
def printNums(n):
str = ""
printRec( str , 0 , n)
if __name__ = = '__main__' :
n = 4
printNums(n)
|
C#
using System;
class GFG {
static void printRec(String number,
int extraOnes,
int remainingPlaces)
{
if (0 == remainingPlaces)
{
Console.Write(number + " " );
return ;
}
printRec(number + "1" , extraOnes + 1,
remainingPlaces - 1);
if (0 < extraOnes)
printRec(number + "0" , extraOnes - 1,
remainingPlaces - 1);
}
static void printNums( int n)
{
String str = "" ;
printRec(str, 0, n);
}
public static void Main()
{
int n = 4;
printNums(n);
}
}
|
PHP
<?php
function printRec( $number , $extraOnes ,
$remainingPlaces )
{
if (0 == $remainingPlaces )
{
echo ( $number . " " );
return ;
}
printRec( $number . "1" , $extraOnes + 1,
$remainingPlaces - 1);
if (0 < $extraOnes )
printRec( $number . "0" , $extraOnes - 1,
$remainingPlaces - 1);
}
function printNums( $n )
{
$str = "" ;
printRec( $str , 0, $n );
}
$n = 4;
printNums( $n );
|
Javascript
<script>
function printRec(number, extraOnes, remainingPlaces)
{
if (0 == remainingPlaces) {
document.write(number + " " );
return ;
}
printRec(number + "1" , extraOnes + 1,
remainingPlaces - 1);
if (0 < extraOnes)
printRec(number + "0" , extraOnes - 1,
remainingPlaces - 1);
}
function printNums(n)
{
let str = "" ;
printRec(str, 0, n);
}
let n = 4;
printNums(n);
</script>
|
Output
1111 1110 1101 1100 1011 1010
Time Complexity: O(n)
Auxiliary Space: O(n)
A non-recursive solution also exists, the idea is to directly generate the numbers in the range of 2N to 2(N-1) then require, only these which satisfies the condition:
Implementation:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
string getBinaryRep( int N, int num_of_bits)
{
string r = "" ;
num_of_bits--;
while (num_of_bits >= 0)
{
if (N & (1 << num_of_bits))
r.append( "1" );
else
r.append( "0" );
num_of_bits--;
}
return r;
}
vector<string> NBitBinary( int N)
{
vector<string> r;
int first = 1 << (N - 1);
int last = first * 2;
for ( int i = last - 1; i >= first; --i)
{
int zero_cnt = 0;
int one_cnt = 0;
int t = i;
int num_of_bits = 0;
while (t)
{
if (t & 1)
one_cnt++;
else
zero_cnt++;
num_of_bits++;
t = t >> 1;
}
if (one_cnt >= zero_cnt)
{
bool all_prefix_match = true ;
int msk = (1 << num_of_bits) - 2;
int prefix_shift = 1;
while (msk)
{
int prefix = (msk & i) >> prefix_shift;
int prefix_one_cnt = 0;
int prefix_zero_cnt = 0;
while (prefix)
{
if (prefix & 1)
prefix_one_cnt++;
else
prefix_zero_cnt++;
prefix = prefix >> 1;
}
if (prefix_zero_cnt > prefix_one_cnt)
{
all_prefix_match = false ;
break ;
}
prefix_shift++;
msk = msk & (msk << 1);
}
if (all_prefix_match)
{
r.push_back(getBinaryRep(i, num_of_bits));
}
}
}
return r;
}
int main()
{
int n = 4;
vector<string> results = NBitBinary(n);
for ( int i = 0; i < results.size(); ++i)
cout << results[i] << " " ;
cout << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static String getBinaryRep( int N, int num_of_bits)
{
String r = "" ;
num_of_bits--;
while (num_of_bits >= 0 )
{
if ((N & ( 1 << num_of_bits))!= 0 )
r += "1" ;
else
r += "0" ;
num_of_bits--;
}
return r;
}
static ArrayList<String> NBitBinary( int N)
{
ArrayList<String> r = new ArrayList<String>();
int first = 1 << (N - 1 );
int last = first * 2 ;
for ( int i = last - 1 ; i >= first; --i)
{
int zero_cnt = 0 ;
int one_cnt = 0 ;
int t = i;
int num_of_bits = 0 ;
while (t > 0 )
{
if ((t & 1 ) != 0 )
one_cnt++;
else
zero_cnt++;
num_of_bits++;
t = t >> 1 ;
}
if (one_cnt >= zero_cnt)
{
boolean all_prefix_match = true ;
int msk = ( 1 << num_of_bits) - 2 ;
int prefix_shift = 1 ;
while (msk > 0 )
{
int prefix = (msk & i) >> prefix_shift;
int prefix_one_cnt = 0 ;
int prefix_zero_cnt = 0 ;
while (prefix > 0 )
{
if ((prefix & 1 )!= 0 )
prefix_one_cnt++;
else
prefix_zero_cnt++;
prefix = prefix >> 1 ;
}
if (prefix_zero_cnt > prefix_one_cnt)
{
all_prefix_match = false ;
break ;
}
prefix_shift++;
msk = msk & (msk << 1 );
}
if (all_prefix_match)
{
r.add(getBinaryRep(i, num_of_bits));
}
}
}
return r;
}
public static void main (String[] args)
{
int n = 4 ;
ArrayList<String> results = NBitBinary(n);
for ( int i = 0 ; i < results.size(); ++i)
System.out.print(results.get(i)+ " " );
System.out.println();
}
}
|
Python3
def getBinaryRep(N, num_of_bits):
r = "";
num_of_bits - = 1
while (num_of_bits > = 0 ):
if (N & ( 1 << num_of_bits)):
r + = ( "1" );
else :
r + = ( "0" );
num_of_bits - = 1
return r;
def NBitBinary(N):
r = []
first = 1 << (N - 1 );
last = first * 2 ;
for i in range (last - 1 ,
first - 1 , - 1 ):
zero_cnt = 0 ;
one_cnt = 0 ;
t = i;
num_of_bits = 0 ;
while (t):
if (t & 1 ):
one_cnt + = 1
else :
zero_cnt + = 1
num_of_bits + = 1
t = t >> 1 ;
if (one_cnt > = zero_cnt):
all_prefix_match = True ;
msk = ( 1 << num_of_bits) - 2 ;
prefix_shift = 1 ;
while (msk):
prefix = ((msk & i) >>
prefix_shift);
prefix_one_cnt = 0 ;
prefix_zero_cnt = 0 ;
while (prefix):
if (prefix & 1 ):
prefix_one_cnt + = 1
else :
prefix_zero_cnt + = 1
prefix = prefix >> 1 ;
if (prefix_zero_cnt >
prefix_one_cnt):
all_prefix_match = False ;
break ;
prefix_shift + = 1
msk = msk & (msk << 1 );
if (all_prefix_match):
r.append(getBinaryRep(i,
num_of_bits));
return r
if __name__ = = "__main__" :
n = 4 ;
results = NBitBinary(n);
for i in range ( len (results)):
print (results[i],
end = " " )
print ()
|
C#
using System;
using System.Collections.Generic;
class GFG{
static string getBinaryRep( int N, int num_of_bits)
{
string r = "" ;
num_of_bits--;
while (num_of_bits >= 0)
{
if ((N & (1 << num_of_bits)) != 0)
r += "1" ;
else
r += "0" ;
num_of_bits--;
}
return r;
}
static List< string > NBitBinary( int N)
{
List< string > r = new List< string >();
int first = 1 << (N - 1);
int last = first * 2;
for ( int i = last - 1; i >= first; --i)
{
int zero_cnt = 0;
int one_cnt = 0;
int t = i;
int num_of_bits = 0;
while (t > 0)
{
if ((t & 1) != 0)
one_cnt++;
else
zero_cnt++;
num_of_bits++;
t = t >> 1;
}
if (one_cnt >= zero_cnt)
{
bool all_prefix_match = true ;
int msk = (1 << num_of_bits) - 2;
int prefix_shift = 1;
while (msk > 0)
{
int prefix = (msk & i) >> prefix_shift;
int prefix_one_cnt = 0;
int prefix_zero_cnt = 0;
while (prefix > 0)
{
if ((prefix & 1)!=0)
prefix_one_cnt++;
else
prefix_zero_cnt++;
prefix = prefix >> 1;
}
if (prefix_zero_cnt > prefix_one_cnt)
{
all_prefix_match = false ;
break ;
}
prefix_shift++;
msk = msk & (msk << 1);
}
if (all_prefix_match)
{
r.Add(getBinaryRep(i, num_of_bits));
}
}
}
return r;
}
static public void Main()
{
int n = 4;
List< string > results = NBitBinary(n);
for ( int i = 0; i < results.Count; ++i)
Console.Write(results[i] + " " );
Console.WriteLine();
}
}
|
Javascript
<script>
function getBinaryRep(N, num_of_bits)
{
let r = "" ;
num_of_bits--;
while (num_of_bits >= 0)
{
if ((N & (1 << num_of_bits))!=0)
r += "1" ;
else
r += "0" ;
num_of_bits--;
}
return r;
}
function NBitBinary(N)
{
let r = [];
let first = 1 << (N - 1);
let last = first * 2;
for (let i = last - 1; i >= first; --i)
{
let zero_cnt = 0;
let one_cnt = 0;
let t = i;
let num_of_bits = 0;
while (t > 0)
{
if ((t & 1) != 0)
one_cnt++;
else
zero_cnt++;
num_of_bits++;
t = t >> 1;
}
if (one_cnt >= zero_cnt)
{
let all_prefix_match = true ;
let msk = (1 << num_of_bits) - 2;
let prefix_shift = 1;
while (msk > 0)
{
let prefix = (msk & i) >> prefix_shift;
let prefix_one_cnt = 0;
let prefix_zero_cnt = 0;
while (prefix > 0)
{
if ((prefix & 1)!=0)
prefix_one_cnt++;
else
prefix_zero_cnt++;
prefix = prefix >> 1;
}
if (prefix_zero_cnt > prefix_one_cnt)
{
all_prefix_match = false ;
break ;
}
prefix_shift++;
msk = msk & (msk << 1);
}
if (all_prefix_match)
{
r.push(getBinaryRep(i, num_of_bits));
}
}
}
return r;
}
let n = 4;
let results = NBitBinary(n);
for (let i = 0; i < results.length; ++i)
document.write(results[i]+ " " );
document.write( "</br>" );
</script>
|
Output
1111 1110 1101 1100 1011 1010
Time Complexity: O(m*n)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...