time complexity-O(n*2^n) , space complexity(n*2^n)*/ 1-total number of subsets will be-(2^n)(where n is array size) 2-upper bound of a k bit number will be-(2^k)-1 3-the binary representation of numbers from 1 to 2^k -1 gives permutations of set bits in k bit number. 4-so we can iterate from 1 to 2^n-1 and each iteraton we can find which permutation it represents.*/
| 4 | 3-the binary representation of numbers from 1 to 2^k -1 gives permutations of set bits in k bit number. |
| 5 | 4-so we can iterate from 1 to 2^n-1 and each iteraton we can find which permutation it represents.*/ |
| 6 | class Solution { |
| 7 | public: |
| 8 | vector<vector<int>> subsets(vector<int>& nums) { |
| 9 | int n=nums.size(); |
| 10 | vector<vector<int>> ans; |
| 11 | vector<int>t; |
| 12 | /*for empty subset*/ |
| 13 | ans.push_back(t); |
| 14 | /*step-4*/ |
| 15 | for(int i=1;i<=(1<<n)-1;i++) |
| 16 | { |
| 17 | int nm=i; |
| 18 | /*stores the subset of ith permutation*/ |
| 19 | vector<int>subi; |
| 20 | /*check which bits are set*/ |
| 21 | /*this section gives the rightmost set bit and then unset it.*/ |
| 22 | while(nm) |
| 23 | { |
| 24 | subi.push_back(nums[__builtin_ctz(nm)]); |
| 25 | nm-=nm&(-nm); |
| 26 | } |
| 27 | ans.push_back(subi); |
| 28 | } |
| 29 | |
| 30 | return ans; |
| 31 | } |
| 32 | }; |
nothing calls this directly
no outgoing calls
no test coverage detected