Finding all subsets of a given set in Java
Last Updated :
28 Feb, 2023
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.
Implementation:
Java
// 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);
}
}
Output{ }
{ a }
{ b }
{ a b }
{ c }
{ a c }
{ b c }
{ a b c }
Time complexity: O(n * (2^n)) as the outer loop runs for O(2^n) and the inner loop runs for O(n).
Auxiliary Space :O(1), since no extra space is used.
Related Post:
Finding all subsets of a Set in C/C++
Similar Reads
Find all Unique Subsets of a given Set Given an array A[] of positive integers, print all the unique non-empty subsets of the array Note: The set can not contain duplicate elements, so any repeated subset should be considered only once in the output. Examples: Input: A[] = {1, 5, 6}Output: {{1}, {1, 5}, {1, 6}, {5}, {5, 6}, {6}, {1, 5,
15+ min read
Given a set, find XOR of the XOR's of all subsets. The question is to find XOR of the XOR's of all subsets. i.e if the set is {1,2,3}. All subsets are : [{1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}]. Find the XOR of each of the subset and then find the XOR of every subset result.We strongly recommend you to minimize your browser and try this yo
5 min read
Print sums of all subsets of a given set Given an array of integers, print sums of all subsets in it. Output sums can be printed in any order.Examples : Input: arr[] = {2, 3}Output: 0 2 3 5Explanation: All subsets of this array are - {{}, {2}, {3}, {2, 3}}, having sums - 0, 2, 3 and 5 respectively.Input: arr[] = {2, 4, 5}Output: 0 2 4 5 6
10 min read
Print all subsets of a given Set or Array Given an array arr of size n, your task is to print all the subsets of the array in lexicographical order.A subset is any selection of elements from an array, where the order does not matter, and no element appears more than once. It can include any number of elements, from none (the empty subset) t
12 min read
Number of distinct subsets of a set Given an array of n distinct elements, count total number of subsets. Examples: Input : {1, 2, 3} Output : 8 Explanation: the array contain total 3 element.its subset are {}, {1}, {2}, {3}, {1, 2}, {2, 3}, {3, 1}, {1, 2, 3}. so the output is 8.. We know number of subsets of set of size n is 2n How d
3 min read