Printing all subsets of {1,2,3,…n} without using array or loop
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:
Java
// Java code to print all subsets // of {1, 2, 3, n} without using // array or loop, just recursion. class GfG { // This recursive function calls subset // function to print the subsets one by one. // numBits --> number of bits needed to // represent the number (simply input value n). // num --> Initially equal to 2 ^ n - 1 and // decreases by 1 every recursion until 0. static void printSubSets(int numOfBits, int num) { if (num >= 0) { System.out.print("{ "); // Print the subset corresponding to // binary representation of num. subset(numOfBits - 1, num, numOfBits); System.out.println("}"); // Call the function recursively to // print the next subset. printSubSets(numOfBits, num - 1); } else return; } // This function recursively prints the // subset corresponding to the binary // representation of num. // nthBit --> nth bit from right side // starting from n and decreases until 0. static void subset(int nthBit, int num, int numOfBits) { if (nthBit >= 0) { // Print number in given subset only // if the bit corresponding to it // is set in num. if ((num & (1 << nthBit)) != 0) { System.out.print(numOfBits - nthBit + " "); } // Check for the next bit subset(nthBit - 1, num, numOfBits); } else return; } // Driver code public static void main(String[] args) { int n = 4; printSubSets(n, (int) (Math.pow(2, n)) -1); } } // This code is contributed by laststringx |
chevron_right
filter_none
Python3
# Python3 code to print all subsets # of {1, 2, 3, …n} without using # array or loop, just recursion. # This recursive function calls subset # function to print the subsets one by one. # numBits --> number of bits needed to # represent the number (simply input value n). # num --> Initially equal to 2 ^ n - 1 and # decreases by 1 every recursion until 0. def printSubsets(numOfBits, num): if num >= 0: print("{", end = " ") # Print the subset corresponding to # binary representation of num. subset(numOfBits-1, num, numOfBits) print("}") # Call the function recursively to # print the next subset. printSubsets(numOfBits, num-1) else: return # This function recursively prints the # subset corresponding to the binary # representation of num. # nthBit --> nth bit from right side # starting from n and decreases until 0. def subset(nthBit, num, numOfBits): if nthBit >= 0: # Print number in given subset only # if the bit corresponding to it # is set in num. if num & (1 << nthBit) != 0: print(numOfBits - nthBit, end = " ") # Check for the next bit subset(nthBit-1, num, numOfBits) else: return # Driver Code n = 4printSubsets(n, 2**n - 1) |
chevron_right
filter_none
C#
// C# code to print all subsets // of {1, 2, 3, n} without using // array or loop, just recursion. using System; class GfG { // This recursive function calls subset // function to print the subsets one by one. // numBits --> number of bits needed to // represent the number (simply input value n). // num --> Initially equal to 2 ^ n - 1 and // decreases by 1 every recursion until 0. static void printSubSets(int numOfBits, int num) { if (num >= 0) { Console.Write("{ "); // Print the subset corresponding to // binary representation of num. subset(numOfBits - 1, num, numOfBits); Console.WriteLine("}"); // Call the function recursively to // print the next subset. printSubSets(numOfBits, num - 1); } else return; } // This function recursively prints the // subset corresponding to the binary // representation of num. // nthBit --> nth bit from right side // starting from n and decreases until 0. static void subset(int nthBit, int num, int numOfBits) { if (nthBit >= 0) { // Print number in given subset only // if the bit corresponding to it // is set in num. if ((num & (1 << nthBit)) != 0) { Console.Write(numOfBits - nthBit + " "); } // Check for the next bit subset(nthBit - 1, num, numOfBits); } else return; } // Driver codeM public static void Main(String[] args) { int n = 4; printSubSets(n, (int) (Math.Pow(2, n)) -1); } } // This code is contributed by Srathore |
chevron_right
filter_none
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: ![]()
Recommended Posts:
- How to loop through an associative array and get the key in PHP?
- How to use loop through an array in JavaScript?
- Subset array sum by generating all the subsets
- Maximum possible difference of two subsets of an array
- Iterate associative array using foreach loop in PHP
- How to remove an array element in a foreach loop?
- Sum of (maximum element - minimum element) for all the subsets of an array.
- How to hide an element when printing a web page using CSS?
- Programs for printing pyramid patterns using recursion
- Subsets having Sum between A and B
- Sum of average of all subsets
- Given a set, find XOR of the XOR's of all subsets.
- Number of subsets with zero sum
- Sum of bitwise OR of all possible subsets of given set
- Sum of bitwise AND of all possible subsets of given set
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



