**Problem:** Find all the subsets of a given set.

Input:S = {a, b, c, d}Output:{}, {a} , {b}, {c}, {d}, {a,b}, {a,c}, {a,d}, {b,c}, {b,d}, {c,d}, {a,b,c}, {a,b,d}, {a,c,d}, {b,c,d}, {a,b,c,d}

The total number of subsets of any given set is equal to 2^ (no. of elements in the set). If we carefully notice it is nothing but binary numbers from 0 to 15 which can be shown as below:

0000 | {} |

0001 | {a} |

0010 | {b} |

0011 | {a, b} |

0100 | {c} |

0101 | {a, c} |

0110 | {b, c} |

0111 | {a, b, c} |

1000 | {d} |

1001 | {a, d} |

1010 | {b, d} |

1011 | {a, b, d} |

1100 | {c, d} |

1101 | {a, c, d} |

1110 | {b, c, d} |

1111 | {a, b, c, d} |

Starting from right, 1 at ith position shows that the ith element of the set is present as 0 shows that the element is absent. Therefore, what we have to do is just generate the binary numbers from 0 to 2^n – 1, where n is the length of the set or the numbers of elements in the set.

`// A Java program to print all subsets of a set ` `import` `java.io.IOException; ` ` ` `class` `Main ` `{ ` ` ` `// Print all subsets of given set[] ` ` ` `static` `void` `printSubsets(` `char` `set[]) ` ` ` `{ ` ` ` `int` `n = set.length; ` ` ` ` ` `// Run a loop for printing all 2^n ` ` ` `// subsets one by one ` ` ` `for` `(` `int` `i = ` `0` `; i < (` `1` `<<n); i++) ` ` ` `{ ` ` ` `System.out.print(` `"{ "` `); ` ` ` ` ` `// Print current subset ` ` ` `for` `(` `int` `j = ` `0` `; j < n; j++) ` ` ` ` ` `// (1<<j) is a number with jth bit 1 ` ` ` `// so when we 'and' them with the ` ` ` `// subset number we get which numbers ` ` ` `// are present in the subset and which ` ` ` `// are not ` ` ` `if` `((i & (` `1` `<< j)) > ` `0` `) ` ` ` `System.out.print(set[j] + ` `" "` `); ` ` ` ` ` `System.out.println(` `"}"` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `char` `set[] = {` `'a'` `, ` `'b'` `, ` `'c'` `}; ` ` ` `printSubsets(set); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

Output:

{ } { a } { b } { a b } { c } { a c } { b c } { a b c }

**Related Post:**

Finding all subsets of a Set in C/C++

This article is contributed by **Nikhil Tekwani.** If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sum of subsets of all the subsets of an array | O(3^N)
- Sum of subsets of all the subsets of an array | O(2^N)
- Sum of subsets of all the subsets of an array | O(N)
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Print all subsets of given size of a set
- Sum of bitwise OR of all possible subsets of given set
- Sum of bitwise AND of all possible subsets of given set
- Sum of all subsets of a set formed by first n natural numbers
- Sum of sum of all subsets of a set formed by first N natural numbers
- Product of all Subsets of a set formed by first N natural numbers
- Perfect Sum Problem (Print all subsets with given sum)
- Sum of squares of all Subsets of given Array
- Sum of values of all possible non-empty subsets of the given array
- Product of values of all possible non-empty subsets of given Array
- Sum of cubes of all Subsets of given Array
- Count number of subsets of a set with GCD equal to a given number
- Count of Subsets of a given Set with element X present in it
- Finding the probability of a state at a given time in a Markov chain | Set 2
- Sum of maximum elements of all subsets