From 915ac7baf33400d0c43053ae8dec845d9a05c7d2 Mon Sep 17 00:00:00 2001 From: Roshni jeewani <89697445+Roshnijeewani3457@users.noreply.github.com> Date: Fri, 19 May 2023 14:59:00 +0530 Subject: [PATCH] Create Sum of subsets --- R-Recursion/Sum of subsets | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 R-Recursion/Sum of subsets diff --git a/R-Recursion/Sum of subsets b/R-Recursion/Sum of subsets new file mode 100644 index 000000000..01b0e9022 --- /dev/null +++ b/R-Recursion/Sum of subsets @@ -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>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; +}