Printing all subsets of {1,2,3,â€¦n} without using array or loop in C program

Given a positive integer n we have to print all the subsets of a set of {1, 2, 3, 4,… n} without using any array or loops.

Like we have given any number say 3 s we have to print all the subset in the set {1, 2, 3} which would be {1 2 3}, {1 2}, {2 3}, {1 3}, {1}, {2}, {3} { }.

But we have to do this without using any loop or an array. So, only the recursion is the possible way to solve this type of problem without using any array or a loop.

Example

Input: 3
Output: { 1 2 3 }{ 1 2 }{ 1 3 }{ 1 }{ 2 3 }{ 2 }{ 3 }{ }
Explanation: The set will be {1 2 3} from which we will find the subsets
Input: 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 }{ }

Approach we will be using to solve the given problem

• Start from num = 2^n -1 upto 0.
• Consider the binary representation of num with n number of 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 stated approach in detail how it works using a simple example −

Assuming the input n = 3, so the problem starts from num = 2^3 - 1 = 7

• Binary Representation of 7 ⇒
 1 1 1
• Corresponing Subset ⇒
 1 2 3

Subtracting 1 from num; num = 6

• Binary Representation of 6 ⇒
 1 1 0
• Corresponing Subset ⇒
 1 2

Subtracting 1 from num; num = 5

• Binary Representation of 5 ⇒
 1 0 1
• Corresponing Subset ⇒
 1 3

Subtracting 1 from num; num = 4

• Binary Representation of 4 ⇒
 1 0 0
• Corresponing Subset ⇒
 1

Likewise we will iterate until num = 0 and print all the subsets.

Algorithm

Start
Step 1 → In function int subset(int bitn, int num, int num_of_bits)
If bitn >= 0
If (num & (1 << bitn)) != 0
Print num_of_bits - bitn
subset(bitn - 1, num, num_of_bits);
Else
Return 0
Return 1
Step 2 → In function int printSubSets(int num_of_bits, int num)
If (num >= 0)
Print "{ "
Call function subset(num_of_bits - 1, num, num_of_bits)
Print "}"
Call function printSubSets(num_of_bits, num - 1)
Else
Return 0
Return 1
Step 3 → In function int main()
Declare and initialize int n = 4
Call fucntionprintSubSets(n, (int) (pow(2, n)) -1)
Stop

Example

#include <stdio.h>
#include <math.h>
// This function recursively prints the
// subset corresponding to the binary
// representation of num.
int subset(int bitn, int num, int num_of_bits) {
if (bitn >= 0) {
// Print number in given subset only
// if the bit corresponding to it
// is set in num.
if ((num & (1 << bitn)) != 0) {
printf("%d ", num_of_bits - bitn);
}
// Check the next bit
subset(bitn - 1, num, num_of_bits);
}
else
return 0;
return 1;
}
//function to print the subsets
int printSubSets(int num_of_bits, int num) {
if (num >= 0) {
printf("{ ");
// Printint the subsets corresponding to
// the binary representation of num.
subset(num_of_bits - 1, num, num_of_bits);
printf("}");
// recursively calling the function to
// print the next subset.
printSubSets(num_of_bits, num - 1);
}
else
return 0;
return 1;
}
//main program
int main() {
int n = 4;
printSubSets(n, (int) (pow(2, n)) -1);
}

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 }{ }