Print N-bit binary numbers having more 1s than 0s | Practice | GeeksforGeeks

Given a positive integer N, the task is to find all the N bit binary numbers having more than or equal 1’s than 0’s for any prefix of the number. 
Example 1:
Input:  N = 2
Output: 11 10
Explanation: 11 and 10 have more than 
or equal 1's than 0's

Example 2:
Input:  N = 3
Output: 111 110 101
Explanation: 111, 110 and 101 have more 
than or equal 1's than 0's

User Task:
Your task is to complete the function NBitBinary() which takes a single number as input and returns the list of strings in decreasing order. You need not take any input or print anything.
Expected Time Complexity: O(|2N|)
Expected Auxiliary Space: O(2N)
Constraints:
1 <= N <= 20
+1
bokonist4 days ago
0.14s C++ solution. Approach is very similar to generate paranthesis question with some added conditions to not generate prefixes
.
.
void generateBinary(int n,vector<string> &ans, int onecount, int zerocount, string curStr)
{
    if(onecount >= zerocount && onecount+zerocount==n)
        ans.push_back(curStr);
    
    if(onecount+zerocount < n)
    {
        generateBinary(n, ans, onecount+1, zerocount, curStr+"1");
        if(onecount>zerocount) // to meet the prefix condition
            generateBinary(n, ans, onecount, zerocount+1, curStr+"0");
    }
}
vector<string> NBitBinary(int N)
{
    // Your code goes here
    vector<string> ans;
    generateBinary(N, ans, 1,0,"1");
    return ans;
}
0
premranjan880461 week ago
C++ solution;
 
   vector<string>v;   void solve(int N, string curr, int one, int zero)   {       if(N==0)       {           v.push_back(curr);           return;       }              solve(N-1,curr+"1",one+1,zero);       if(one>zero)       solve(N-1,curr+"0",one,zero+1);   }   vector<string> NBitBinary(int N){    solve(N,"",0,0);    return v;}
0
amanasati11 month ago
Python
class Solution:
    def solve(self,n,curr,ones,zeroes,s):
        if zeroes<=ones and n==ones+zeroes:
            s.append(curr)
            return 
        else:
            if ones+zeroes<n:
                self.solve(n,curr+"1",ones+1,zeroes,s)
            if ones>zeroes:
                self.solve(n,curr+"0",ones,zeroes+1,s)
        
	def NBitBinary(self, N):
		# code here
        ones=0
        zeroes=0
        S=[]
        self.solve(N,"",ones,zeroes,S)
        return S
0
shrustis1761 month ago
C++ solution
 
class Solution{public:
void solve(int one, int zero, int n, string op, vector<string> &v){   if(n==0)   {       v.push_back(op);       return;   }      string op1=op;   op1.push_back('1');   solve(one+1, zero, n-1, op1, v);      if(one>zero)   {       string op2=op;       op2.push_back('0');       solve( one, zero+1, n-1, op2, v);   }      return;}
vector<string> NBitBinary(int N){    // Your code goes here    string op="";    int one=0, zero=0;    vector<string> v;        solve(one, zero, N, op, v);        return v;}};
0
praveenshakya1 month ago
C++ Solution
Total Time Taken:
0.2/1.2
 void helper(int one, int zero, int n, vector<string>&binary, string output){       if(n == 0){           binary.push_back(output);           return;       }       string op1 = output;       op1.push_back('1');       helper(one+1, zero, n-1, binary, op1);              if(one > zero){           string op2 = output;           op2.push_back('0');           helper(one, zero+1, n-1, binary, op2);       }       return;          }
vector<string> NBitBinary(int N){    // Your code goes here    int one = 0;    int zero = 0;    vector<string>binary;    string output = "";        helper(one, zero, N, binary, output);        return binary;}
0
praveenshakya1 month ago
C++ Solution
Total Time Taken:
0.2/1.2
void helper(int one, int zero, int n, vector<string>&binary, string output){       if(n == 0){           binary.push_back(output);           return;       }              string op1 = output;       op1.push_back('1');       helper(one+1, zero, n-1, binary, op1);              if(one > zero){           string op2 = output;           op2.push_back('0');           helper(one, zero+1, n-1, binary, op2);       }       return;          }   vector<string> NBitBinary(int N){    // Your code goes here    int one = 0, zero = 0;    vector<string>binary;    string output = "";        helper(one, zero, N, binary, output);        return binary;}
0
sk77887752 months ago
class Solution {
    ArrayList<String> NBitBinary(int n) {
        // code here
        ArrayList<String> res=new ArrayList<>();
        solve(res,"",n,0,-1);
        return res;
    }
    
    void solve(ArrayList<String> res,String str,int n,int n_1,int n_0) {
        if(n==0) {
            res.add(str);
            return;
        }
        solve(res,str+"1",n-1,n_1+1,n_0);
        if(n_1-n_0>1)
            solve(res,str+"0",n-1,n_1,n_0+1);
    }
}
0
swapnilroy22Premium2 months ago
Simply soln using recurssion and observation
class Solution {
    ArrayList<String> NBitBinary(int N) {
        // code here
        int count_one=0,count_zero=0;
        ArrayList<String>res=new ArrayList<>();
        generateNBitBinary("",count_one,count_zero,N,res);
        return res;
    }
    public void generateNBitBinary(String op,int c1,int c0,int N,ArrayList<String>ans){
        if(c1+c0==N)
        {
            ans.add(op);
            return;
        }
        if(c1<N){
            generateNBitBinary(op+"1",c1+1,c0,N,ans);
        }
        if(c1>c0)
        {
            generateNBitBinary(op+"0",c1,c0+1,N,ans);
        }
    }
}
0
abro3 months ago
class Solution{public:
   void find(int o, int z,int N,string ans,vector<string>&v ){       if(N==0)       {           v.push_back(ans);           return;       }       if(o==z)       {           string op1;           op1=ans;           op1.push_back('1');           find(o+1,z,N-1,op1,v);       }        if(o>z)       {           string op1,op2;           op1=ans;           op2=ans;           op1.push_back('1');           op2.push_back('0');           find(o+1,z,N-1,op1,v);           find(o,z+1,N-1,op2,v);       }   }
vector<string> NBitBinary(int N){    // Your code goes here    vector<string>v;    string ans="";    int o=0;    int z=0;    find(o,z,N,ans,v);    return v;}};
0
kittusingh04243 months ago
    void Binary(int N,int one,int zero,string ans,vector<string> &v)
    {
        if(N==0)
        {
            v.push_back(ans);
            return;
        }
        string op=ans;
        op.push_back('1');
        Binary(N-1,one+1,zero,op,v);
        if(one>zero)
        {
            string op1=ans;
            op1.push_back('0');
            Binary(N-1,one,zero+1,op1,v);
        }
        
    }
	vector<string> NBitBinary(int N)
	{
	    // Your code goes here
	    vector<string> v;
	    if(N==0)    return v;
	    string ans;
	    int one=0;
	    int zero=0;
	    Binary(N,one,zero,ans,v);
	    return v;
	    
	}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.