416. Partition Equal Subset Sum

#dp

Problem

Intuition

It is same as CN: Subset Sum Equal To K. The only extra constraint is if it can be partitioned into two equal halves. We sum the whole array, if total sum is odd then it cannot be evenly distributed and we will return false.

Then we take the totalSum/2 and search if we can find any subset whose target is equal to totalSum/2. If we find half sum then it means the rest of the elements make up the half sum and can be returned true;

Time and Space Complexity

I have already used the most optimised version for space.

  • Time Complexity: O(N^K) traverse all the states

  • Space Complexity: O(K+1) Store only the previous state values

Solution

Last updated