AlgoMaster Logo

Partition Equal Subset Sum

Ashish

Ashish Pratap Singh

medium

Problem Description

Solve it on LeetCode

Approaches

1. Recursive Approach

Intuition:

The problem can be seen as a variation of the 0/1 knapsack problem. The most basic approach is to use recursion to try every possible subset and check if there is a subset with a sum that is equal to half of the total sum of the array. We are using a recursive function that tries to include or exclude each number.

Code:

2. Memoization Approach

Intuition:

The recursive approach has an exponential time complexity because it recalculates results for the same subproblems multiple times. We can optimize it by storing intermediate results in a memoization table.

Code:

3. Dynamic Programming Approach

Intuition:

We can solve the problem iteratively using dynamic programming with a boolean array. We aim to determine if we can form the subset with sum totalSum/2, by iteratively updating our possibilities in a boolean array.

Code: