Tap here to switch tabs
Problem
Submissions
Solution

Solution: Partition Equal Subset Sum

Statement

Naive approach

We can solve this problem with the following two steps:

  1. First, we calculate the sum of the array. If the sum of the array is odd, there can’t be two subsets with an equal sum, so we return FALSE.
  2. If the sum is even, we calculate sum/2sum/2 and find a subset of the array with a sum equal to sum/2sum/2.

The naive approach is to solve the second step using recursion. In this approach, we calculate the result repeatedly every time. For example, consider an array, [1, 6, 20, 7, 8], which can be partitioned into [1, 20] and [6, 7, 8] with the sum 21. While computing the sum, we encounter 21 twice: once in the first subset (6 + 7 = 13, 13 + 8 = 21) and once in the second subset (1 + 20 = 21). However, since we’re not storing these sums, it is computed twice.

The time complexity of this approach is O(2n)O(2^n). In the worst case, for each element in an array, this solution tests two possibilities, i.e., whether to include or exclude it. We can avoid the repeated work done in the naive approach by storing the result calculated at each step. We store all the values in a lookup table.

Optimized approach using dynamic programming

We use the bottom-up approach of dynamic programming, also known as the tabulation technique. In this approach, the smallest problem is solved, the result is saved, and larger subproblems are computed based on the evaluated results. The problem is divided into subproblems, which are dependent on each other. We start by initializing a lookup table and setting up the values of the base cases. For every subsequent, larger subproblem, we fetch the results of the required preceding smaller subproblems and use them to get the solution to the current subproblem.

Here is how we implement this algorithm:

  1. First, we calculate the sum of the array, nums. If the sum of the array is odd, there can’t be two subsets with an equal sum, so we return FALSE.

  2. Create a lookup table, dp, of size (((s/2)+1)×(n+1))(((s/2)+1) \times (n+1)) ...

Tap here to switch tabs
Problem
Submissions
Solution

Solution: Partition Equal Subset Sum

Statement

Naive approach

We can solve this problem with the following two steps:

  1. First, we calculate the sum of the array. If the sum of the array is odd, there can’t be two subsets with an equal sum, so we return FALSE.
  2. If the sum is even, we calculate sum/2sum/2 and find a subset of the array with a sum equal to sum/2sum/2.

The naive approach is to solve the second step using recursion. In this approach, we calculate the result repeatedly every time. For example, consider an array, [1, 6, 20, 7, 8], which can be partitioned into [1, 20] and [6, 7, 8] with the sum 21. While computing the sum, we encounter 21 twice: once in the first subset (6 + 7 = 13, 13 + 8 = 21) and once in the second subset (1 + 20 = 21). However, since we’re not storing these sums, it is computed twice.

The time complexity of this approach is O(2n)O(2^n). In the worst case, for each element in an array, this solution tests two possibilities, i.e., whether to include or exclude it. We can avoid the repeated work done in the naive approach by storing the result calculated at each step. We store all the values in a lookup table.

Optimized approach using dynamic programming

We use the bottom-up approach of dynamic programming, also known as the tabulation technique. In this approach, the smallest problem is solved, the result is saved, and larger subproblems are computed based on the evaluated results. The problem is divided into subproblems, which are dependent on each other. We start by initializing a lookup table and setting up the values of the base cases. For every subsequent, larger subproblem, we fetch the results of the required preceding smaller subproblems and use them to get the solution to the current subproblem.

Here is how we implement this algorithm:

  1. First, we calculate the sum of the array, nums. If the sum of the array is odd, there can’t be two subsets with an equal sum, so we return FALSE.

  2. Create a lookup table, dp, of size (((s/2)+1)×(n+1))(((s/2)+1) \times (n+1)) ...