CN: Subset Sum Equal To K

#dp

Problem

Intuition

Time and Space Complexity

Approach
Time Complexity
Space Complexity
Explaination

Memoization

O(N^K)

O(N^K) + O(N)

N^k is the number of states. O(N) -> Recursion Stack

Tabulation

O(N^K)

O(N^K)

We store only the states computation. No recursion stack

Space Optimized Tabulation

O(N^K)

O(K+1)

Only the target possible states array is required

Base Cases understanding in Tabulation

The first column of the table will always be set to true because no matter what the value is in that given index, we can always choose not to pick up resulting in achieving the target sum.

And another base case is

When we check if the first element of the element is less than or equal to target sum, then it means the arr[0] can be part of the subset with which we can achieve the target sum.

The reason we are especially checking for arr[0] is we start exploring the states from index=1

Solution

Last updated