Print first n numbers with exactly two set bits
Given a number n, print first n positive integers with exactly two set bits in their binary representation.
Examples :
Input: n = 3
Output: 3 5 6
The first 3 numbers with two set bits are 3 (0011),
5 (0101) and 6 (0110)
Input: n = 5
Output: 3 5 6 9 10 12
A Simple Solution is to consider all positive integers one by one starting from 1. For every number, check if it has exactly two sets bits. If a number has exactly two set bits, print it and increment count of such numbers.
An Efficient Solution is to directly generate such numbers. If we clearly observe the numbers, we can rewrite them as given below pow(2,1)+pow(2,0), pow(2,2)+pow(2,0), pow(2,2)+pow(2,1), pow(2,3)+pow(2,0), pow(2,3)+pow(2,1), pow(2,3)+pow(2,2), ………
All numbers can be generated in increasing order according to higher of two set bits. The idea is to fix higher of two bits one by one. For current higher set bit, consider all lower bits and print the formed numbers.
C++
#include <iostream>
using namespace std;
void printTwoSetBitNums( int n)
{
int x = 1;
while (n > 0)
{
int y = 0;
while (y < x)
{
cout << (1 << x) + (1 << y) << " " ;
n--;
if (n == 0)
return ;
y++;
}
x++;
}
}
int main()
{
printTwoSetBitNums(4);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void printTwoSetBitNums( int n)
{
int x = 1 ;
while (n > 0 )
{
int y = 0 ;
while (y < x)
{
System.out.print((( 1 << x) + ( 1 << y)) + " " );
n--;
if (n == 0 )
return ;
y++;
}
x++;
}
}
public static void main (String[] args)
{
int n = 4 ;
printTwoSetBitNums(n);
}
}
|
Python3
def printTwoSetBitNums(n) :
x = 1
while (n > 0 ) :
y = 0
while (y < x) :
print (( 1 << x) + ( 1 << y),
end = " " )
n - = 1
if (n = = 0 ) :
return
y + = 1
x + = 1
printTwoSetBitNums( 4 )
|
C#
using System;
class GFG
{
static void printTwoSetBitNums( int n)
{
int x = 1;
while (n > 0)
{
int y = 0;
while (y < x)
{
Console.Write(((1 << x) +
(1 << y)) + " " );
n--;
if (n == 0)
return ;
y++;
}
x++;
}
}
public static void Main()
{
int n = 4;
printTwoSetBitNums(n);
}
}
|
Javascript
<script>
function printTwoSetBitNums(n)
{
let x = 1;
while (n > 0)
{
let y = 0;
while (y < x)
{
document.write((1 << x) + (1 << y) + " " );
n--;
if (n == 0)
return ;
y++;
}
x++;
}
}
printTwoSetBitNums(4);
</script>
|
PHP
<?php
function printTwoSetBitNums( $n )
{
$x = 1;
while ( $n > 0)
{
$y = 0;
while ( $y < $x )
{
echo (1 << $x ) + (1 << $y ), " " ;
$n --;
if ( $n == 0)
return ;
$y ++;
}
$x ++;
}
}
printTwoSetBitNums(4);
?>
|
Output :
3 5 6 9
Time Complexity : O(n)
Auxiliary Space: O(1)
Approach#2: Using while and join
The approach is to start from the integer 3 and check whether the number of set bits in its binary representation is equal to 2 or not. If it has exactly 2 set bits, then add it to the list of numbers with 2 set bits until the list has n elements.
Algorithm
1. Initialize an empty list res to store the integers with exactly two set bits.
2. Initialize an integer variable i to 3.
3. While the length of the list res is less than n, do the following:
a. Check whether the number of set bits in the binary representation of i is equal to 2 or not using the count() method of the string.
b. If the number of set bits is equal to 2, then append i to the list res.
c. Increment i by 1.
4. Return the list res.
C++
#include <iostream>
#include <vector>
using namespace std;
int countSetBits( int num) {
int count = 0;
while (num > 0) {
count += num & 1;
num >>= 1;
}
return count;
}
vector< int > numbersWithTwoSetBits( int n) {
vector< int > res;
int i = 3;
while (res.size() < n) {
if (countSetBits(i) == 2) {
res.push_back(i);
}
i++;
}
return res;
}
int main() {
int n = 3;
vector< int > result = numbersWithTwoSetBits(n);
cout << "Result: " ;
for ( int i = 0; i < result.size(); i++) {
cout << result[i] << " " ;
}
cout << endl;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class GFG {
static int countSetBits( int num)
{
int count = 0 ;
while (num > 0 ) {
count += num & 1 ;
num >>= 1 ;
}
return count;
}
static List<Integer> numbersWithTwoSetBits( int n)
{
List<Integer> res = new ArrayList<>();
int i = 3 ;
while (res.size() < n) {
if (countSetBits(i)
== 2 ) {
res.add(
i);
}
i++;
}
return res;
}
public static void main(String[] args)
{
int n = 3 ;
List<Integer> result = numbersWithTwoSetBits(
n);
for ( int num : result) {
System.out.print(
num + " " );
}
System.out.println();
}
}
|
Python3
def numbersWithTwoSetBits(n):
res = []
i = 3
while len (res) < n:
if bin (i).count( '1' ) = = 2 :
res.append(i)
i + = 1
return res
n = 3
result = numbersWithTwoSetBits(n)
output_string = ' ' .join( str (x) for x in result)
print (output_string)
|
C#
using System;
using System.Collections.Generic;
class Program
{
static int CountSetBits( int num)
{
int count = 0;
while (num > 0)
{
count += num & 1;
num >>= 1;
}
return count;
}
static List< int > NumbersWithTwoSetBits( int n)
{
List< int > res = new List< int >();
int i = 3;
while (res.Count < n)
{
if (CountSetBits(i) == 2)
{
res.Add(i);
}
i++;
}
return res;
}
static void Main( string [] args)
{
int n = 3;
List< int > result = NumbersWithTwoSetBits(n);
Console.Write( "Result: " );
foreach ( int num in result)
{
Console.Write(num + " " );
}
Console.WriteLine();
}
}
|
Javascript
function countSetBits(num) {
let count = 0;
while (num > 0) {
count += num & 1;
num >>= 1;
}
return count;
}
function numbersWithTwoSetBits(n) {
let res = [];
let i = 3;
while (res.length < n) {
if (countSetBits(i) === 2) {
res.push(i);
}
i++;
}
return res;
}
let n = 3;
let result = numbersWithTwoSetBits(n);
console.log(result.join( ' ' ));
|
Time Complexity: O(n log n), where n is the number of integers with exactly two set bits. This is because we are checking the number of set bits in the binary representation of each integer, which takes O(log n) time.
Space Complexity: O(n), where n is the number of integers with exactly two set bits. This is because we are storing the list of integers with two set bits in memory.
Last Updated :
12 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...