-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Sum of subsets
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |