Skip to content

Commit

Permalink
Sum of Subsets
Browse files Browse the repository at this point in the history
Create Sum of subsets
  • Loading branch information
Lakhankumawat authored May 24, 2023
2 parents 6758c03 + 915ac7b commit 4b20e14
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions R-Recursion/Sum of subsets
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Subset_Sum
{
public:
// RECURSIVE METHOD
bool subsetSum_Recursive(int arr[], int n, int sum)
{
if (sum == 0)
return true
if (n < 0 || sum < 0)
return false;
bool include = subsetSum_Recursive(arr, n - 1, sum - arr[n]);
bool exclude = subsetSum_Recursive(arr, n - 1, sum);
return include || exclude;
}
};

int main()
{
int i, n, sum;
Subset_Sum S;
cout << "Enter the number of elements in the set" << endl;
cin >> n;
int a[n];
cout << "Enter the values" << endl;
for(i=0;i<n;i++)
cin>>a[i];
cout << "Enter the value of sum" << endl;
cin >> sum;
bool f = false;
S.subsetSum_Recursive(a, n, sum);
if (f)
cout << "subset with the given sum found" << endl;
else
cout << "no required subset found" << endl;
return 0;
}

0 comments on commit 4b20e14

Please sign in to comment.