Power set
Problem
Backtracking approach:
TC:(2^n) i.e. exponential time complexity (Since we are left with two choice at every recursive call i.e. either to consider the value at 'index' or not that leads to 2 possible outcome, this will happen for n times)
SC:(2^n)*(n), n for temp ArrayList<>() and 2^n for the main ArrayList<>();
class Solution { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> list = new ArrayList<>(); powerSet(nums,0,list,new ArrayList<Integer>()); return list; } public void powerSet(int [] nums, int index , List<List<Integer>> list, List<Integer> l){ //base case if(index ==nums.length){ list.add(new ArrayList<>(l)); return; } //take l.add(nums[index]); //consider the value at 'index' powerSet(nums,index+1,list,l); //dont take; l.remove(l.size()-1);// don't consider the value at 'index' powerSet(nums,index+1,list,l); } }
Using Bit Manipulation:
TC: O(2^n)*n
SC: O(2^n)*n, (2^n for the main list, and n for the subset lists, well not all the subsets will be of size n but still we can assume that is the case)
pre-requisite: check if ith bit is set or not ( refer the Bit manipulation tips and tricks page for more details)
Intuition:
If all the no . subsets are represented as binary values,
for example : if n = 3 i.e. array having 3 value in it.
there will be 2^n = 8 subsets
8 subsets can also be represented as
index 2 | index 1 | index 0 | subset number |
---|---|---|---|
0 | 0 | 0 | 0 |
0 | 0 | 1 | 1 |
0 | 1 | 0 | 2 |
0 | 1 | 1 | 3 |
1 | 0 | 0 | 4 |
1 | 0 | 1 | 5 |
1 | 1 | 0 | 6 |
1 | 1 | 1 | 7 |
We will take into consideration that if bit value is 1 then that index value in the nums[] should be taken into consideration for forming the subset.
This way we will be able to create all the subsets
class Solution { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> list = new ArrayList<>(); int n = nums.length; int noOfSubset = 1<<n; // this is nothing but 2^n, i.e if there are n elements in the array, they will form 2^n subsets for(int num = 0;num<noOfSubset;num++){ /// all possible subsets numbers List<Integer> l = new ArrayList<>(); for(int i =0;i<n;i++){// for the given subset number find which index value to pick if((num & (1<<i))!=0) l.add(nums[i]); } list.add(l); } return list; } }
The above is the detailed content of Power set. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...
