Printing all subsets of {1,2,3,…n} without using array or loop
Last Updated :
12 Sep, 2022
Given a natural number n, print all the subsets of the set without using any array or loop (only the use of recursion is allowed).
Examples:
Input : n = 4
Output : { 1 2 3 4 }
{ 1 2 3 }
{ 1 2 4 }
{ 1 2 }
{ 1 3 4 }
{ 1 3 }
{ 1 4 }
{ 1 }
{ 2 3 4 }
{ 2 3 }
{ 2 4 }
{ 2 }
{ 3 4 }
{ 3 }
{ 4 }
{ }
Input : n = 2
Output : { 1 2 }
{ 1 }
{ 2 }
{ }
Approach:
- Start from upto 0.
- Consider the binary representation of num with n bits.
- Start from the leftmost bit which represents 1, the second bit represents 2, and so on until nth bit which represents n.
- Print the number corresponding to the bit if it is set.
- Perform the above steps for all values of num until it is equal to 0.
Let’s understand the above approach through an example:
Considering input n = 4, start from .
and so on … until num = 0.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void subset( int , int , int );
void printSubsets( int numOfBits, int num)
{
if (num >= 0)
{
cout << "{ " ;
subset(numOfBits - 1, num, numOfBits);
cout << "}" << endl;
printSubsets(numOfBits, num - 1);
}
else
return ;
}
void subset( int nthBit, int num, int numOfBits)
{
if (nthBit >= 0)
{
if (num & (1 << nthBit))
{
cout << numOfBits - nthBit << " " ;
}
subset(nthBit - 1, num, numOfBits);
}
else
return ;
}
int main()
{
int n = 4;
printSubsets(n, pow (2, n) - 1);
}
|
Java
class GfG
{
static void printSubSets( int numOfBits, int num)
{
if (num >= 0 )
{
System.out.print( "{ " );
subset(numOfBits - 1 , num, numOfBits);
System.out.println( "}" );
printSubSets(numOfBits, num - 1 );
} else
return ;
}
static void subset( int nthBit, int num, int numOfBits)
{
if (nthBit >= 0 )
{
if ((num & ( 1 << nthBit)) != 0 )
{
System.out.print(numOfBits - nthBit + " " );
}
subset(nthBit - 1 , num, numOfBits);
} else
return ;
}
public static void main(String[] args)
{
int n = 4 ;
printSubSets(n, ( int ) (Math.pow( 2 , n)) - 1 );
}
}
|
Python3
def printSubsets(numOfBits, num):
if num > = 0 :
print ( "{" , end = " " )
subset(numOfBits - 1 , num, numOfBits)
print ( "}" )
printSubsets(numOfBits, num - 1 )
else :
return
def subset(nthBit, num, numOfBits):
if nthBit > = 0 :
if num & ( 1 << nthBit) ! = 0 :
print (numOfBits - nthBit, end = " " )
subset(nthBit - 1 , num, numOfBits)
else :
return
n = 4
printSubsets(n, 2 * * n - 1 )
|
C#
using System;
class GfG
{
static void printSubSets( int numOfBits, int num)
{
if (num >= 0)
{
Console.Write( "{ " );
subset(numOfBits - 1, num, numOfBits);
Console.WriteLine( "}" );
printSubSets(numOfBits, num - 1);
} else
return ;
}
static void subset( int nthBit, int num, int numOfBits)
{
if (nthBit >= 0)
{
if ((num & (1 << nthBit)) != 0)
{
Console.Write(numOfBits - nthBit + " " );
}
subset(nthBit - 1, num, numOfBits);
} else
return ;
}
public static void Main(String[] args)
{
int n = 4;
printSubSets(n, ( int ) (Math.Pow(2, n)) -1);
}
}
|
Javascript
<script>
function printSubsets(numOfBits, num)
{
if (num >= 0)
{
document.write( "{ " );
subset(numOfBits - 1, num, numOfBits);
document.write( "}<br>" );
printSubsets(numOfBits, num - 1);
}
else
return ;
}
function subset(nthBit, num, numOfBits)
{
if (nthBit >= 0)
{
if (num & (1 << nthBit))
{
document.write( numOfBits - nthBit + " " );
}
subset(nthBit - 1, num, numOfBits);
}
else
return ;
}
var n = 4;
printSubsets(n, Math.pow(2, n) - 1);
</script>
|
Output: { 1 2 3 4 }
{ 1 2 3 }
{ 1 2 4 }
{ 1 2 }
{ 1 3 4 }
{ 1 3 }
{ 1 4 }
{ 1 }
{ 2 3 4 }
{ 2 3 }
{ 2 4 }
{ 2 }
{ 3 4 }
{ 3 }
{ 4 }
{ }
Time Complexity:
Auxiliary Space: O(n) for call stack
Share your thoughts in the comments
Please Login to comment...