Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- // bool recursivecheck(int curr, int end, vector<int>& v, int target, int &tempsum)
- // {
- // //base case
- // if (tempsum == target) return true;
- // if (curr == end) return false;
- // //let's include this
- // tempsum += v[curr];
- // if (recursivecheck(curr + 1, end, v, target, tempsum) == true) return true;
- // //let's exclude this
- // tempsum -= v[curr];
- // if (recursivecheck(curr + 1, end, v, target, tempsum) == true) return true;
- // return false;
- // }
- void printsubsets(int curr, int end, vector<int>& v, vector<int>& temp)
- {
- //base case
- if (curr == end + 1)
- {
- if (temp.size() == 0) cout << "Empty Subset";
- for (auto i : temp) cout << i << " ";
- cout << endl;
- return;
- }
- //let's include this
- temp.push_back(v[curr]);
- printsubsets(curr + 1, end, v, temp);
- //let's exclude this
- temp.pop_back();
- printsubsets(curr + 1, end, v, temp);
- return;
- }
- void printuniquesubsets(int curr, int end, vector<int>& v, vector<int>& temp,
- set<vector<int>>& s)
- {
- //base case
- if (curr == end + 1)
- {
- if (temp.size() == 0)
- {
- cout << "Empty Subset"; return ;
- }
- if (s.find(temp) != s.end()) return;//don't include this
- else s.insert(temp);//include this
- return;
- }
- //let's include this
- temp.push_back(v[curr]);
- printuniquesubsets(curr + 1, end, v, temp, s);
- //let's exclude this
- temp.pop_back();
- printuniquesubsets(curr + 1, end, v, temp, s);
- return;
- }
- int main()
- {
- // int n, target; cin >> n >> target;
- // vector<int> v(n);
- // for (int i = 0; i < n; i++) cin >> v[i];
- // int tempsum = 0;
- // if (recursivecheck(0, n, v, target, tempsum))
- // {
- // cout << "Found the subset" << endl;
- // }
- // else cout << "Not Found The subset" << endl;
- int n; cin >> n;
- vector<int> v(n);
- for (int i = 0; i < n; i++) cin >> v[i];
- sort(v.begin(), v.end());
- vector<int> temp;
- set<vector<int>> s;
- // printsubsets(0, n - 1, v, temp);
- printuniquesubsets(0, n - 1, v, temp, s);
- for (auto i : s)
- {
- //i->vector
- for (auto j : i) cout << j << " ";
- cout << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement