Partition Equal Subset Sum in DSA Python - Time & Space Complexity
We want to know how the time needed to check if an array can be split into two equal-sum parts grows as the array gets bigger.
How does the number of steps change when the array size or sum increases?
Analyze the time complexity of the following code snippet.
def can_partition(nums):
total = sum(nums)
if total % 2 != 0:
return False
target = total // 2
dp = [False] * (target + 1)
dp[0] = True
for num in nums:
for j in range(target, num - 1, -1):
dp[j] = dp[j] or dp[j - num]
return dp[target]
This code checks if the list can be split into two parts with equal sums using a dynamic programming approach.
- Primary operation: Nested loops - outer loop over each number, inner loop over possible sums up to target.
- How many times: Outer loop runs n times (n = number of elements), inner loop runs up to target sum times.
As the number of elements (n) or the target sum (half of total) grows, the steps increase roughly by multiplying these two.
| Input Size (n) | Approx. Operations (n * target) |
|---|---|
| 10 (sum ~ 100) | 10 * 50 = 500 |
| 100 (sum ~ 1000) | 100 * 500 = 50,000 |
| 1000 (sum ~ 10,000) | 1000 * 5000 = 5,000,000 |
Pattern observation: The time grows roughly with the product of the number of elements and the target sum, so doubling either roughly doubles the work.
Time Complexity: O(n * sum)
This means the time needed grows with both the number of elements and the total sum of the array.
[X] Wrong: "The time depends only on the number of elements (n)."
[OK] Correct: The sum value also affects the time because the inner loop runs up to the target sum, so bigger sums mean more steps.
Understanding this time complexity helps you explain how dynamic programming solves subset problems efficiently compared to checking all subsets.
"What if we used recursion with memoization instead of this bottom-up approach? How would the time complexity change?"
Practice
Solution
Step 1: Understand the problem statement
The problem asks if the input list can be divided into two parts where the sum of numbers in both parts is the same.Step 2: Identify the key condition
This means the total sum of the list must be even and there must be a subset with sum equal to half of the total sum.Final Answer:
If the list can be split into two subsets with equal sums -> Option BQuick Check:
Partition Equal Subset Sum = split into equal sum subsets [OK]
- Thinking it checks for duplicates
- Assuming list must be sorted
- Confusing with even number check
dp of size target + 1 with false values, except dp[0] set to true?Solution
Step 1: Understand list initialization
We want a list of size target+1 with all False except dp[0] = True.Step 2: Check each option
dp = [False] * (target + 1); dp[0] = True correctly creates a list of False of length target+1 and sets dp[0] to True.Final Answer:
dp = [False] * (target + 1); dp[0] = True -> Option DQuick Check:
Initialize dp with False, set dp[0] True [OK]
- Using wrong list size (target instead of target+1)
- Setting dp[0] to False instead of True
- Using list comprehension with wrong range
nums = [1, 5, 11, 5]
total = sum(nums)
target = total // 2
dp = [False] * (target + 1)
dp[0] = True
for num in nums:
for j in range(target, num - 1, -1):
dp[j] = dp[j] or dp[j - num]
print(dp[target])Solution
Step 1: Calculate total and target
Total sum = 1+5+11+5 = 22, target = 11.Step 2: Trace dp updates
dp array tracks if sum j is possible. After processing all nums, dp[11] becomes True because subset [11] or [5,5,1] sums to 11.Final Answer:
True -> Option AQuick Check:
Subset sum 11 possible = True [OK]
- Forgetting to iterate backwards in inner loop
- Miscomputing total sum
- Confusing dp meaning (True means sum possible)
def can_partition(nums):
total = sum(nums)
if total % 2 != 0:
return False
target = total // 2
dp = [False] * target
dp[0] = True
for num in nums:
for j in range(target, num - 1, -1):
dp[j] = dp[j] or dp[j - num]
return dp[target]Solution
Step 1: Check dp list size
dp list is created with size target, but indices go up to target, so size must be target + 1.Step 2: Identify impact of bug
Accessing dp[target] causes IndexError because dp size is too small.Final Answer:
dp list size should be target + 1, not target -> Option CQuick Check:
dp size = target + 1 needed [OK]
- Using dp size = target causes index error
- Changing loop direction incorrectly
- Setting dp[0] to False breaks base case
nums = [2, 2, 3, 5], which of the following statements is true regarding the Partition Equal Subset Sum problem?Solution
Step 1: Calculate total sum and target
Total sum = 2+2+3+5 = 12, target = 6.Step 2: Check possible subsets summing to 6
Possible subsets: [2,2,2] no (only two 2s), [3,3] no (only one 3), [5,1] no 1 missing. No subset sums to 6 exactly.Step 3: Verify partition
Since no subset sums to 6, partition into equal sum subsets is not possible.Final Answer:
It's not possible to partition into two subsets with equal sum -> Option AQuick Check:
No subset sums to half total sum = no partition [OK]
- Assuming subsets like [2,3] and [2,5] sum equally
- Ignoring that subsets must sum exactly half
- Miscounting elements in subsets
Ready to prove it?
Pass the test and this pattern moves unknown
