Subset Sum Problem Using Backtracking Approach.: Code
Subset Sum Problem Using Backtracking Approach.: Code
approach.
By Devansh Kumar (2K19/MC/037)
Code:
#include<bits/stdc++.h>
using namespace std;
int freq = 0;
bool inc = false;
for (int j = i; j < n; j++){
if (a[i] == a[j]){
freq++;
}
}
for (int j = freq; j >= 0; j--){
for (int k = 0; k < j; k++){
sol.push_back(a[i]);
}
if (inc){
return true;
}
int main() {
int n;
cout << "Enter No of Nos. in set: ";
cin >> n;
int a[n];
cout << "\nEnter Nos. one-by one: ";
for (int i = 0; i < n; i++){
cin >> a[i];
}
sort(a, a + n);
vector<int>sol;
int t;
cout << "\nEnter the Target Sum: ";
cin >> t;
cout << "\nThe Subsets are : ";
Output: