0
0
DSA Typescriptprogramming~10 mins

Partition Equal Subset Sum in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Partition Equal Subset Sum
Calculate total sum of array
Check if total sum is odd?
YesReturn false: cannot partition
No
Set target = total sum / 2
Use DP to check if subset sums to target
If subset found
Return true
No
Return false
We first find the total sum. If it's odd, partition is impossible. Otherwise, we try to find a subset with sum equal to half the total using dynamic programming.
Execution Sample
DSA Typescript
function canPartition(nums: number[]): boolean {
  const total = nums.reduce((a, b) => a + b, 0);
  if (total % 2 !== 0) return false;
  const target = total / 2;
  const dp = new Array(target + 1).fill(false);
  dp[0] = true;
  for (const num of nums) {
    for (let j = target; j >= num; j--) {
      dp[j] = dp[j] || dp[j - num];
    }
  }
  return dp[target];
}
This code checks if the array can be split into two subsets with equal sum by using a boolean DP array to track achievable sums.
Execution Table
StepOperationCurrent NumberDP Array StateExplanation
1Calculate total sum--Sum of [1, 5, 11, 5] is 22
2Check if sum is odd22-22 is even, continue
3Set target22-Target sum is 11
4Initialize dp array-[true, false, false, false, false, false, false, false, false, false, false, false]dp[0] = true, others false
5Process number 11[true, true, false, false, false, false, false, false, false, false, false, false]dp[1] = dp[1] || dp[0] = true
6Process number 55[true, true, false, false, false, true, true, false, false, false, false, false]dp[5] and dp[6] updated
7Process number 1111[true, true, false, false, false, true, true, false, false, false, false, true]dp[11] updated to true
8Process number 55[true, true, false, false, false, true, true, false, false, false, true, true]dp[10] updated to true
9Check dp[target]--dp[11] is true, partition possible
10Return result-trueFunction returns true
💡 dp[target] is true, meaning a subset with sum 11 exists, so partition is possible
Variable Tracker
VariableStartAfter Step 5After Step 6After Step 7After Step 8Final
total02222222222
target-1111111111
dp[true, false, false, false, false, false, false, false, false, false, false, false][true, true, false, false, false, false, false, false, false, false, false, false][true, true, false, false, false, true, true, false, false, false, false, false][true, true, false, false, false, true, true, false, false, false, false, true][true, true, false, false, false, true, true, false, false, false, true, true][true, true, false, false, false, true, true, false, false, false, true, true]
current number-15115-
result-----true
Key Moments - 3 Insights
Why do we check if the total sum is odd before proceeding?
Because if the total sum is odd (see Step 2 in execution_table), it's impossible to split the array into two equal subsets, so we return false immediately.
Why do we iterate backwards in the inner loop when updating dp?
Iterating backwards (from target down to current number) prevents using the same number multiple times in one iteration, ensuring each number is only counted once (see Steps 5-8).
What does dp[j] = dp[j] || dp[j - num] mean in this context?
It means we mark sum j as achievable if it was already achievable or if sum j - num was achievable before adding current number num (see Steps 5-8).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 6, what is the value of dp[5]?
Atrue
Bfalse
Cundefined
Dnull
💡 Hint
Check the DP Array State column at Step 6 in execution_table
At which step does dp[11] become true?
AStep 5
BStep 7
CStep 6
DStep 8
💡 Hint
Look at the DP Array State column in execution_table rows for dp[11]
If the total sum was 23 instead of 22, what would happen at Step 2?
AContinue to DP processing
BSet target to 11.5
CReturn false immediately
DThrow an error
💡 Hint
Refer to the check for odd sum in Step 2 of execution_table
Concept Snapshot
Partition Equal Subset Sum:
- Calculate total sum of array
- If sum is odd, return false
- Set target = sum / 2
- Use boolean DP array dp of size target+1
- dp[0] = true (empty subset)
- For each number, update dp backwards
- Return dp[target] indicating if subset sum exists
Full Transcript
The Partition Equal Subset Sum problem checks if an array can be split into two subsets with equal sums. First, we sum all numbers. If the sum is odd, partitioning is impossible. If even, we set the target as half the sum. We use a boolean dynamic programming array dp where dp[j] means whether a subset sum j is achievable. We initialize dp[0] as true because sum zero is always possible with an empty subset. For each number, we update dp from the target down to the number, marking sums achievable by including that number. If dp[target] is true at the end, it means a subset with sum equal to half the total exists, so partitioning is possible. Otherwise, it is not.