diff --git a/Python/count_subsets_with_sum_k.py b/Python/count_subsets_with_sum_k.py new file mode 100644 index 00000000..51aa4848 --- /dev/null +++ b/Python/count_subsets_with_sum_k.py @@ -0,0 +1,13 @@ +def findWays(nums, k): + MOD = 10**9 + 7 + n = len(nums) + + dp = [0] * (k + 1) + dp[0] = 1 # There is always 1 way to make sum = 0 (choose nothing) + + for num in nums: + # Iterate backwards to avoid reusing the same element twice + for target in range(k, num - 1, -1): + dp[target] = (dp[target] + dp[target - num]) % MOD + + return dp[k] \ No newline at end of file