Can someone do continue this . C++ Write a function to compute all k-subsets of
ID: 3842500 • Letter: C
Question
Can someone do continue this . C++ Write a function to compute all k-subsets of a given set. Recall that k-subsets are subsets with precisely k elements. Use function prototypes below. And write some test code. Do not copy and paste codes from google please!
Thank you
#include <iostream>
#include <vector>
#include <set>
using std::cout;
using std::vector;
using std::set;
set<set<int>>ksubsets(set<int>&S,size_t k);
vector<vector<int>>ksubsets(vector<int>&v , size_t k);
int main(){
return 0;}
Elle Edit Search view Tools options Language Buffers Help 1 maps.cpp 2 recursion cpp 3 morerecursion.cpp 4 powerset.cpp 5 ksubsets.cpp using std: :cout include vector 4 using std: vector; 5 #includeExplanation / Answer
#include<bits/stdc++.h>
#include <iostream>
using namespace std;
void generateSubsets(vector<int> &S, int index, vector<int> ¤t, vector<vector<int> > &result,int k) {
if (index >= S.size()) {
if(current.size()==k)
result.push_back(current);
return;
}
// Ignore the current index.
generateSubsets(S, index + 1, current, result,k);
// Include the current index.
current.push_back(S[index]);
generateSubsets(S, index + 1, current, result,k);
current.pop_back();
}
vector<vector<int> >compute(vector<int> &S,int k) {
vector<vector<int> > result;
vector<int> current;
sort(S.begin(), S.end());
generateSubsets(S, 0, current, result,k);
sort(result.begin(), result.end());
return result;
}
int main() {
vector<int> S;
int size_set;
int a,i;
cout << "enter the size of the set ";
cin >> size_set;
for(i=0;i<size_set;i++)
{
cin >>a;
S.push_back(a);
}
int k;
cout << "enter the value of K ";
cin >> k;
vector<vector<int> > answer =compute(S,k);
for (i = 0; i < answer.size(); i++)
{
for (int j = 0; j < answer[i].size(); j++)
{
cout << answer[i][j] << " ";
}
cout << endl;
}
return 0;
}
for running the code , you have to ener the size of the original set and then followed by that no. of elements . then you have to enter the value of k and you will get your output.
eg.
1 2 3 4 5