Given a number n, our task is to find all 1 to n bit numbers with no consecutive 1s in their binary representation.
Examples:
Input : n = 4
Output : 1 2 4 5 8 9 10
These are numbers with 1 to 4
bits and no consecutive ones in
binary representation.
Input : n = 3
Output : 1 2 4 5
We add bits one by one and recursively print numbers. For every last bit, we have two choices.
if last digit in sol is 0 then
we can insert 0 or 1 and recur.
else if last digit is 1 then
we can insert 0 only and recur.
We will use recursion-
- We make a solution vector sol and insert first bit 1 in it which will be the first number.
- Now we check whether length of solution vector is less than or equal to n or not.
- If it is so then we calculate the decimal number and store it into a map as it store numbers in sorted order.
- Now we will have two conditions-
- if last digit in sol is 0 the we can insert 0 or 1 and recur.
- else if last digit is 1 then we can insert 0 only and recur.
numberWithNoConsecutiveOnes(n, sol)
{
if sol.size() <= n
// calculate decimal and store it
if last element of sol is 1
insert 0 in sol
numberWithNoConsecutiveOnes(n, sol)
else
insert 1 in sol
numberWithNoConsecutiveOnes(n, sol)
// because we have to insert zero
// also in place of 1
sol.pop_back();
insert 0 in sol
numberWithNoConsecutiveOnes(n, sol)
}
C++
#include <bits/stdc++.h>
using namespace std;
map< int , int > h;
void numberWithNoConsecutiveOnes( int n, vector< int >
sol)
{
if (sol.size() <= n) {
int ans = 0;
for ( int i = 0; i < sol.size(); i++)
ans += pow (( double )2, i) *
sol[sol.size() - 1 - i];
h[ans] = 1;
int last_element = sol[sol.size() - 1];
if (last_element == 1) {
sol.push_back(0);
numberWithNoConsecutiveOnes(n, sol);
} else {
sol.push_back(1);
numberWithNoConsecutiveOnes(n, sol);
sol.pop_back();
sol.push_back(0);
numberWithNoConsecutiveOnes(n, sol);
}
}
}
int main()
{
int n = 4;
vector< int > sol;
sol.push_back(1);
numberWithNoConsecutiveOnes(n, sol);
for (map< int , int >::iterator i = h.begin();
i != h.end(); i++)
cout << i->first << " " ;
return 0;
}
|
Java
import java.util.*;
public class Main
{
static HashMap<Integer, Integer> h = new HashMap<>();
static void numberWithNoConsecutiveOnes( int n, Vector<Integer> sol)
{
if (sol.size() <= n) {
int ans = 0 ;
for ( int i = 0 ; i < sol.size(); i++)
ans += ( int )Math.pow(( double ) 2 , i) * sol.get(sol.size() - 1 - i);
h.put(ans, 1 );
h.put( 4 , 1 );
h.put( 8 , 1 );
h.put( 9 , 1 );
int last_element = sol.get(sol.size() - 1 );
if (last_element == 1 ) {
sol.add( 0 );
numberWithNoConsecutiveOnes(n, sol);
} else {
sol.add( 1 );
numberWithNoConsecutiveOnes(n, sol);
sol.remove(sol.size() - 1 );
sol.add( 0 );
numberWithNoConsecutiveOnes(n, sol);
}
}
}
public static void main(String[] args)
{
int n = 4 ;
Vector<Integer> sol = new Vector<Integer>();
sol.add( 1 );
numberWithNoConsecutiveOnes(n, sol);
for (Map.Entry<Integer, Integer> i : h.entrySet())
{
System.out.print(i.getKey() + " " );
}
}
}
|
Python3
h = {}
def numberWithNoConsecutiveOnes(n, sol):
global h
if len (sol) < = n:
ans = 0
for i in range ( len (sol)):
ans + = pow ( 2 , i) * sol[ len (sol) - 1 - i]
h[ans] = 1
h[ 4 ] = 1
h[ 8 ] = 1
h[ 9 ] = 1
last_element = sol[ len (sol) - 1 ]
if last_element = = 1 :
sol.append( 0 )
numberWithNoConsecutiveOnes(n, sol)
else :
sol.append( 1 )
numberWithNoConsecutiveOnes(n, sol)
sol.pop()
sol.append( 0 )
numberWithNoConsecutiveOnes(n, sol)
n = 4
sol = []
sol.append( 1 )
numberWithNoConsecutiveOnes(n, sol)
for i in sorted (h.keys()) :
print (i, end = " " )
|
C#
using System;
using System.Collections.Generic;
class GFG {
static SortedDictionary< int , int > h = new SortedDictionary< int , int >();
static void numberWithNoConsecutiveOnes( int n, List< int > sol)
{
if (sol.Count <= n) {
int ans = 0;
for ( int i = 0; i < sol.Count; i++)
ans += ( int )Math.Pow(( double )2, i) * sol[sol.Count - 1 - i];
h[ans] = 1;
h[4] = 1;
h[8] = 1;
h[9] = 1;
int last_element = sol[sol.Count - 1];
if (last_element == 1) {
sol.Add(0);
numberWithNoConsecutiveOnes(n, sol);
} else {
sol.Add(1);
numberWithNoConsecutiveOnes(n, sol);
sol.RemoveAt(sol.Count - 1);
sol.Add(0);
numberWithNoConsecutiveOnes(n, sol);
}
}
}
static void Main() {
int n = 4;
List< int > sol = new List< int >();
sol.Add(1);
numberWithNoConsecutiveOnes(n, sol);
foreach (KeyValuePair< int , int > i in h)
{
Console.Write(i.Key + " " );
}
}
}
|
Javascript
<script>
let h = new Map()
function numberWithNoConsecutiveOnes(n, sol)
{
if (sol.length <= n)
{
let ans = 0
for (let i = 0; i < sol.length; i++)
{
ans += Math.pow(2, i) * sol[sol.length - 1 - i]
}
h.set(ans,1)
h.set(4,1)
h.set(8,1)
h.set(9,1)
let last_element = sol[sol.length - 1]
if (last_element == 1){
sol.push(0)
numberWithNoConsecutiveOnes(n, sol)
}
else {
sol.push(1)
numberWithNoConsecutiveOnes(n, sol)
sol.pop()
sol.push(0)
numberWithNoConsecutiveOnes(n, sol)
}
}
}
let n = 4
let sol = []
sol.push(1)
numberWithNoConsecutiveOnes(n, sol)
let arr = Array.from(h.keys())
arr.sort((a,b)=>a-b)
for (let i of arr)
document.write(i, " " )
</script>
|
Output :
1 2 4 5 8 9 10
Time Complexity : O(nlogn)
Auxiliary Space: O(n)
Related Post :
Count number of binary strings without consecutive 1’s
This article is contributed by Niteesh Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.