Challenge - 5 Problems
Partition Equal Subset Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Partition Equal Subset Sum Check
What is the output of this Rust function when called with nums = [1, 5, 11, 5]?
DSA Rust
fn can_partition(nums: Vec<i32>) -> bool { let total: i32 = nums.iter().sum(); if total % 2 != 0 { return false; } let target = total / 2; let mut dp = vec![false; (target + 1) as usize]; dp[0] = true; for num in nums { for j in (num..=target).rev() { dp[j as usize] = dp[j as usize] || dp[(j - num) as usize]; } } dp[target as usize] } fn main() { let nums = vec![1, 5, 11, 5]; println!("{}", can_partition(nums)); }
Attempts:
2 left
💡 Hint
Check if the total sum is even and then try to find a subset with sum equal to half of total.
✗ Incorrect
The total sum is 22, which is even. The function checks if a subset sums to 11. The subset [11] or [5, 5, 1] sums to 11, so the function returns true.
🧠 Conceptual
intermediateUnderstanding the DP Array in Partition Equal Subset Sum
In the Partition Equal Subset Sum problem, what does the boolean dp array represent?
Attempts:
2 left
💡 Hint
Think about what the dp array tracks as you iterate through numbers.
✗ Incorrect
dp[j] being true means there exists at least one subset of the input numbers that sums exactly to j.
🔧 Debug
advancedIdentify the Bug in Partition Equal Subset Sum Implementation
What error will this Rust code produce when run with nums = [2, 2, 3, 5]?
DSA Rust
fn can_partition(nums: Vec<i32>) -> bool { let total: i32 = nums.iter().sum(); if total % 2 != 0 { return false; } let target = total / 2; let mut dp = vec![false; (target + 1) as usize]; dp[0] = true; for num in nums { for j in num..=target { dp[j as usize] = dp[j as usize] || dp[(j - num) as usize]; } } dp[target as usize] } fn main() { let nums = vec![2, 2, 3, 5]; println!("{}", can_partition(nums)); }
Attempts:
2 left
💡 Hint
Check the direction of the inner loop and how dp is updated.
✗ Incorrect
The inner loop iterates forward (without .rev()), causing it to use already updated values in the same iteration. This turns it into an unbounded knapsack, allowing multiple uses of numbers. For [2,2,3,5], total=12, target=6, but no subset sums to 6; however, DP incorrectly sets dp[6]=true (e.g., 2+2+2), so returns true.
🚀 Application
advancedNumber of Subsets with Equal Sum Partition
Given nums = [2, 2, 3, 5], how many subsets sum to half of the total sum?
Attempts:
2 left
💡 Hint
Calculate total sum, then find subsets summing to half of it.
✗ Incorrect
Total sum is 12, half is 6. Subsets summing to 6 are [2,2,2] (not possible here) but [2,2,3] sums to 7, no. Actually subsets are [2,2,2] no, [3,3] no. [2,2,3] sums to 7, no. [5,2] sums to 7 no. [5,3] sums to 8 no. [2,3] sums to 5 no. [2,5] sums to 7 no. [2,2] sums to 4 no. [3] sums to 3 no. [5] sums to 5 no. So no subsets sum to 6. So answer is 0.
❓ Predict Output
expertOutput of Optimized Partition Equal Subset Sum with Early Exit
What is the output of this Rust code when nums = [1, 2, 5, 11, 5]?
DSA Rust
fn can_partition(nums: Vec<i32>) -> bool { let total: i32 = nums.iter().sum(); if total % 2 != 0 { return false; } let target = total / 2; let mut dp = vec![false; (target + 1) as usize]; dp[0] = true; for num in nums { for j in (num..=target).rev() { dp[j as usize] = dp[j as usize] || dp[(j - num) as usize]; } if dp[target as usize] { return true; } } dp[target as usize] } fn main() { let nums = vec![1, 2, 5, 11, 5]; println!("{}", can_partition(nums)); }
Attempts:
2 left
💡 Hint
Check the total sum and if any subset sums to half of it.
✗ Incorrect
Total sum is 24, half is 12. Subsets that sum to 12 exist, e.g., [1, 11]. The function returns true.
