From 87421da8554f8750e166090681d8ad2c1ada98f8 Mon Sep 17 00:00:00 2001 From: Vipanshu Suman <73050057+vipu18@users.noreply.github.com> Date: Thu, 2 Oct 2025 13:40:43 +0530 Subject: [PATCH] Implement subset sum problem using dynamic programming --- .../count_subsets_with_sum_k.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 CPP/dynamic-programming/count_subsets_with_sum_k.cpp diff --git a/CPP/dynamic-programming/count_subsets_with_sum_k.cpp b/CPP/dynamic-programming/count_subsets_with_sum_k.cpp new file mode 100644 index 0000000..10f8d17 --- /dev/null +++ b/CPP/dynamic-programming/count_subsets_with_sum_k.cpp @@ -0,0 +1,45 @@ +#include +#include +using namespace std; + +// Paste your Solution class here (choose one implementation) +class Solution { + int mod = 1000000007; +public: + int perfectSum(vector& arr, int k) { + int n = arr.size(); + vectorprev(k+1, 0); + vectorcurr(k+1, 0); + curr[0] = 1; + prev[0] = 1; + if(arr[0]<=k){ + prev[arr[0]] = 1; + } + for(int i = 1; i> n >> k; + vector arr(n); + cout << "Enter array elements: "; + for(int i=0; i> arr[i]; + + Solution sol; + int result = sol.perfectSum(arr, k); + cout << "Number of subsets with sum " << k << ": " << result << endl; + return 0; +}