[AlgoExpert] Powerset

Powerset

  • Time : O(n * 2^n)
  • Space : O(n * 2^n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <vector>
using namespace std;

vector<vector<int>> powerset(vector<int> array) {
vector<vector<int>> res;
res.emplace_back();
for(auto& a : array) {
int sz = res.size();
for(int i = 0; i < sz; i++) {
auto set = res[i];
set.push_back(a);
res.push_back(set);
}
}

return res;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/14/PS/AlgoExpert/powerset/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.