Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- void printKPartition(int level, int n, int k, vector<vector<int> > v){
- //answer valid only when k = 0 & level > n
- if(level > n){
- if(k == 0){
- for(vector<int> i : v){
- cout << "[";
- for(int j : i){
- cout << ' ' << j;
- }
- cout << ']';
- }
- cout << '\n';
- }
- return;
- }
- //can create a different partition
- if(k != 0){
- vector<int> partition;
- partition.push_back(level);
- v.push_back(partition);
- printKPartition(level + 1, n, k - 1, v);
- v.pop_back();
- }
- //don't create a separate partition, go in existing ones
- //NOTE - if there is no existing subset, don't call
- if(v.size() != 0){
- for(int i = 0; i < v.size(); i++){
- v[i].push_back(level);
- printKPartition(level + 1, n, k, v);
- v[i].pop_back();
- }
- }
- }
- int main() {
- // your code goes here
- int n, k;
- cin >> n >> k;
- vector<vector<int> > v;
- printKPartition(1, n , k, v);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement