Find all subsets of set (power set) in java

If you want to practice data structure and algorithm programs, you can go through Java coding interview questions.

In this post, we will see how to find all subsets of set or power set in java.


Problem

Given a set of distinct integers, arr, return all possible subsets (the power set).

For example:

Input: nums = [1,2,3] Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[] ]

We will use two approaches here.


Using recursion

You can find all subsets of set or power set using recursion. Here is the simple approach.

  • As each recursion call will represent subset here, we will add resultList(see recursion code below) to the list of subsets in each call.
  • Iterate over elements of a set.
  • In each iteration
    • Add elements to the list
    • explore(recursion) and make start = i+1 to go through remaining elements of the array.
    • Remove element from the list.

Here is java code for recursion.

Output

[] [1] [1, 2] [1, 2, 1] [1, 1] [2] [2, 1] [1]

Let’s understand with the help of a diagram.

subset helper recursion

If you notice, each node(resultList) represent subset here.

Using bit manipulation

You can find all subsets of set or power set using iteration as well.

There will be 2^N subsets for a given set, where N is the number of elements in set.
For example, there will be 2^4 = 16 subsets for the set {1, 2, 3, 4}.

Each ‘1’ in the binary representation indicate an element in that position.
For example, the binary representation of number 6 is 0101 which in turn represents the subset {1, 3} of the set {1, 2, 3, 4}.

subset binary

How can we find out which bit is set for binary representation, so that we can include the element in the subset?

To check if 0th bit is set, you can do logical & with 1

To check if 1st bit is set, you can do logical & with 2

To check if 2nd bit is set, you can do logical & with 2^2

.

.

To check if nth bit is set, you can do logical & with 2^n.

Let’s say with the help of example:

For a set {1 ,2, 3}

0 1 1  &  0 0 1 = 1    –> 1 will be included in subset

0 1 1  &  0 1 0 = 1    –> 2 will be included in subset

0 1 1  &  1 0  0 =0   –> 3 will not be included in subset.

Here is java code for the bit approach.

Output

{ }
{ 1 }
{ 2 }
{ 1 2 }
{ 3 }
{ 1 3 }
{ 2 3 }
{ 1 2 3 }

Here is complete representation of bit manipulation approach.

Subset bit manipulation

That’s all about finding all subsets of set(power set).

Related Posts

  • Maximum number of vowels in a Substring of given length
    18 June

    Maximum Number of Vowels in a Substring of Given Length

    Table of ContentsApproach – 1 Generate All Substrings Using substring() MethodApproach – 2 Using Sliding Window Method (Linear Time Solution) In this article, we will look at an interesting problem related to the Strings and [Sliding-Window Algorithm](https://java2blog.com/sliding-window-maximum-java/ “Sliding-Window Algorithm”). The problem is : "Given a String we have to Find the Maximum Number of Vowel […]

  • Find first and last position of element in sorted array
    04 June

    Search for a range Leetcode – Find first and last position of element in sorted array

    Table of ContentsApproach 1 (Using Linear Search)Approach 2 (Using Modified Binary Search-Optimal) In this article, we will look into an interesting problem asked in Coding Interviews related to Searching Algorithms. The problem is: Given a Sorted Array, we need to find the first and last position of an element in Sorted array. This problem is […]

  • 30 April

    Convert Postfix to Infix in Java

    Learn about how to convert Postfix to Infix in java.

  • Prefix to postfix in java
    30 April

    Convert Prefix to Postfix in Java

    Learn about how to convert Prefix to Postfix in java.

  • Data structures in java
    16 April

    Data Structures in java

    Table of ContentsArray Declare and initialize array in javaAdvantages of arrayDisadvantages of array ExampleArray practice programsStackStack implementation using ArrayStack implementation using LinkedListImplementationPractice ProgramsQueueQueue implementation using arrayQueue implementation using LinkedListImplementationLinkedListImplementationLinkedList Practice ProgramsBinary treeImplementationBinary tree practice programsBinary Search treeImplementationBinary search tree Practice programsTrieImplementationHeapImplementationGraphImplementation Inbuild data structures in javaStringHashMapLinkedHashMapArrayListLinkedListHashSet In this post, we will see about various data […]

  • 29 November

    Top 100+ Java coding interview questions

    Table of ContentsStringQuestion 1 : How to reverse a String in java? Can you write a program without using any java inbuilt methods?Question 2 : Write a java program to check if two Strings are anagram in java?Question 3 : Write a program to check if String has all unique characters in java?Question 4 : […]

Comments

  1. Hey,

    Amazing job and a great explanation. Keep up the good work. You are helping the community. I completely support you.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.