14. Perfect Sum Problem
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
I have solved the Perfect Sum Problem using recursion with memoization. Here's how my approach works:
We define a recursive function solve
that takes in the current index i
, the remaining sum sum
, and a memoization table dp
. This function calculates the number of ways to obtain the given sum
using elements from the array arr
starting from index i
.
Base case:
If sum
becomes 0, we have found a valid combination, so we return 1 to count it.
If we have reached the end of the array arr
(i >= n), or if sum
becomes negative, we return 0
as there are no more elements to consider or the sum cannot be achieved.
We check if the result for the current state (i, sum) is already calculated and stored in the dp
array. If so, we return the cached result to avoid redundant calculations.
We initialize two variables t
and nt
to count the number of ways by taking and not-taking the current element arr[i]
, respectively.
If the current element arr[i]
is less than or equal to sum
, we can include it in the sum. So, we recursively call the solve
function for the next index i + 1
and the reduced sum sum - arr[i]
, and add this count to t
.
We also recursively call the solve
function for the next index i + 1
and the same sum sum
, and add this count to nt
.
Finally, we update the dp
array with the total count for the current state (i, sum) as (t + nt) % mod
, where mod
is a constant defined as 1e9 + 7. We return this count.
Time Complexity: The time complexity of this solution is O(n * sum)
, where n
is the number of elements in the arr
array, and sum
is the target sum. This is because we have a recursive function with two parameters, and we memoize the results, so each state is calculated only once.
Auxiliary Space Complexity: The auxiliary space complexity is O(n * sum)
as well, due to the memoization table dp
, which stores the results for all possible combinations of indices and sums.
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the repository.