📖 Scenario: You have a collection of items with weights. You want to see if you can split them into two groups that weigh the same.This is like trying to divide a set of gifts equally between two friends so both get the same total weight.
🎯 Goal: Build a program that checks if the list of weights can be split into two groups with equal sum.
📋 What You'll Learn
Create a vector called nums with the exact values: 1, 5, 11, 5
Create an integer variable called totalSum that stores the sum of all numbers in nums
Write a function called canPartition that takes nums and returns true if it can be split into two equal sum subsets, otherwise false
Print the result of canPartition(nums) as "true" or "false"
💡 Why This Matters
🌍 Real World
Dividing resources or tasks evenly is common in logistics, budgeting, and load balancing.
💼 Career
Understanding partition problems helps in optimization, resource allocation, and algorithm design roles.
Progress0 / 4 steps
1
Create the list of weights
Create a vector of integers called nums with these exact values: 1, 5, 11, 5
DSA C++
Hint
Use std::vector<int> and initialize it with the given numbers inside curly braces.
2
Calculate the total sum of weights
Create an integer variable called totalSum that stores the sum of all numbers in nums using a for loop
DSA C++
Hint
Start totalSum at 0, then add each number in nums using a range-based for loop.
3
Write the function to check partition possibility
Write a function called canPartition that takes const std::vector<int>& nums and returns bool. Inside, return false if totalSum is odd. Otherwise, use a boolean vector dp of size target + 1 where target = totalSum / 2. Set dp[0] to true. For each number in nums, update dp from target down to the number, setting dp[j] to dp[j] || dp[j - num]. Finally, return dp[target].
DSA C++
Hint
Use dynamic programming with a boolean vector to track reachable sums. Update from the back to avoid reuse in the same iteration.
4
Print the result of partition check
Print "true" if canPartition(nums) returns true, otherwise print "false"
DSA C++
Hint
Use std::cout to print "true" or "false" depending on the function result.
Practice
(1/5)
1. What does the Partition Equal Subset Sum problem ask you to determine about a given array of positive integers?
easy
A. If the array can be split into two subsets with equal sums
B. If the array contains any duplicate elements
C. If the array is sorted in ascending order
D. If the array contains only even numbers
Solution
Step 1: Understand the problem statement
The problem asks whether the array can be divided into two parts where the sum of elements in both parts is the same.
Step 2: Identify the key condition
This means the total sum of the array must be even, and there must be a subset with sum equal to half of the total sum.
Final Answer:
If the array can be split into two subsets with equal sums -> Option A
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 sorting is required
Confusing with subset sum existence
2. Which of the following C++ code snippets correctly initializes a boolean DP array for the Partition Equal Subset Sum problem with size (sum/2 + 1) and sets the base case?
easy
A. bool dp[sum / 2 + 1]; std::fill(dp, dp + sum / 2 + 1, false); dp[0] = true;
D. std::vector<bool> dp(sum + 1, false); dp[sum] = true;
Solution
Step 1: Choose correct data type and size
We need a boolean vector of size (sum/2 + 1) to track subset sums. std::vector<bool> dp(sum / 2 + 1, false); dp[0] = true; uses correct size and type.
Step 2: Set base case correctly
dp[0] = true means sum zero is always possible. std::vector<bool> dp(sum / 2 + 1, false); dp[0] = true; does this correctly. std::vector<int> dp(sum / 2 + 1, 0); dp[0] = 1; uses int vector, which is less appropriate. std::vector<bool> dp(sum + 1, false); dp[sum] = true; uses wrong size and sets dp[sum] instead of dp[0].
Change inner loop to iterate backwards: for (int j = target; j >= num; --j) -> Option D
Quick Check:
Backward iteration in DP loop fixes state update [OK]
Hint: Iterate dp array backwards to avoid overwriting states [OK]
Common Mistakes:
Iterating dp forwards causing wrong results
Incorrect dp initialization
Wrong sum parity check
5. Given the array {2, 2, 3, 5}, which of the following statements is true regarding the Partition Equal Subset Sum problem?
hard
A. The array can be partitioned into two subsets with equal sum: {2,3} and {2,5}
B. The array cannot be partitioned into two subsets with equal sum because no subset sums to half the total
C. The array can be partitioned into two subsets with equal sum: {2,2,3} and {5}
D. The array cannot be partitioned into two subsets with equal sum because total sum is odd
Solution
Step 1: Calculate total sum
Sum = 2 + 2 + 3 + 5 = 12, which is even, so partition might be possible.
Step 2: Check for subset with sum 6
Half sum = 6. Possible subsets:
- {2,2,3} sum = 7 (too high)
- {2,3} sum = 5 (too low)
- {5} sum = 5 (too low)
- {2,2} sum = 4 (too low)
No subset sums exactly to 6.
Final Answer:
The array cannot be partitioned into two subsets with equal sum because no subset sums to half the total -> Option B
Quick Check:
Half sum subset missing = No partition [OK]
Hint: Check if half sum subset exists before concluding [OK]
Common Mistakes:
Assuming total sum even means partition possible
Incorrect subset sums calculation
Confusing subsets with unequal sums
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.