📖 Scenario: Imagine you have a collection of weights and you want to see if you can split them into two groups that weigh exactly the same. This is useful when packing bags evenly or sharing items fairly.
🎯 Goal: You will write a program that checks if a list of numbers can be divided into two parts with equal sums.
📋 What You'll Learn
Create a list called weights with the exact values: 1, 5, 11, 5
Create a variable called total_weight that stores the sum of all weights
Write a function called can_partition that takes weights and returns True if the list can be split into two equal sum subsets, otherwise False
Print the result of calling can_partition(weights)
💡 Why This Matters
🌍 Real World
Balancing loads, dividing resources evenly, or fair sharing of items.
💼 Career
Understanding subset sum problems helps in optimization, resource allocation, and algorithm design roles.
Progress0 / 4 steps
1
Create the list of weights
Create a list called weights with these exact values: 1, 5, 11, 5
DSA Python
Hint
Use square brackets to create a list and separate numbers with commas.
2
Calculate the total weight
Create a variable called total_weight that stores the sum of all numbers in the weights list using the sum() function
DSA Python
Hint
Use sum(weights) to add all numbers in the list.
3
Write the partition check function
Write a function called can_partition that takes weights as input and returns True if the list can be split into two subsets with equal sum, otherwise False. Use these steps inside the function: 1. Calculate the total sum of weights. 2. If the total sum is odd, return False immediately. 3. Create a set called possible_sums starting with 0. 4. For each weight in weights, update possible_sums by adding the weight to each existing sum. 5. If half of the total sum is in possible_sums, return True, else False.
DSA Python
Hint
Use a set to keep track of sums you can make. Add each weight to all sums you already have.
4
Print the result of partition check
Print the result of calling can_partition(weights) to see if the list can be split into two equal sum subsets
DSA Python
Hint
Use print(can_partition(weights)) to show the result.
Practice
(1/5)
1. What does the Partition Equal Subset Sum problem ask you to determine about a given list of positive integers?
easy
A. If the list contains any duplicate numbers
B. If the list can be split into two subsets with equal sums
C. If the list is sorted in ascending order
D. If the list contains only even numbers
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 B
Quick Check:
Partition Equal Subset Sum = split into equal sum subsets [OK]
Hint: Check if total sum is even to start [OK]
Common Mistakes:
Thinking it checks for duplicates
Assuming list must be sorted
Confusing with even number check
2. Which of the following Python code snippets correctly initializes a boolean list dp of size target + 1 with false values, except dp[0] set to true?
easy
A. dp = [True for _ in range(target + 1)]; dp[0] = True
B. dp = [True] * (target + 1); dp[0] = False
C. dp = [False for _ in range(target)]; dp[0] = True
D. dp = [False] * (target + 1); dp[0] = 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.
Hint: Use [False] * size and set dp[0] True manually [OK]
Common Mistakes:
Using wrong list size (target instead of target+1)
Setting dp[0] to False instead of True
Using list comprehension with wrong range
3. What will be the output of the following Python code snippet?
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])
medium
A. True
B. False
C. IndexError
D. SyntaxError
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 A
Quick Check:
Subset sum 11 possible = True [OK]
Hint: Check if half sum subset exists using dp array [OK]
Common Mistakes:
Forgetting to iterate backwards in inner loop
Miscomputing total sum
Confusing dp meaning (True means sum possible)
4. The following code attempts to solve the Partition Equal Subset Sum problem but has a bug. What is the bug?
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]
medium
A. dp[0] should be False initially
B. The total sum check should be for odd numbers
C. dp list size should be target + 1, not target
D. The inner loop should iterate forwards, not backwards
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 C
Quick Check:
dp size = target + 1 needed [OK]
Hint: dp size must cover index target inclusive [OK]
Common Mistakes:
Using dp size = target causes index error
Changing loop direction incorrectly
Setting dp[0] to False breaks base case
5. Given the list nums = [2, 2, 3, 5], which of the following statements is true regarding the Partition Equal Subset Sum problem?
hard
A. It's not possible to partition into two subsets with equal sum
B. It's possible to partition into two subsets with equal sum: [2,2,3] and [5]
C. It's possible to partition into two subsets with equal sum: [2,3] and [2,5]
D. The problem requires subsets to have different sums
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 A
Quick Check:
No subset sums to half total sum = no partition [OK]
Hint: Sum list, check if half sum subset exists [OK]
Common Mistakes:
Assuming subsets like [2,3] and [2,5] sum equally
Ignoring that subsets must sum exactly half
Miscounting elements in subsets
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.