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 the following Go code that checks if the array can be partitioned into two subsets with equal sum?
DSA Go
package main import "fmt" func canPartition(nums []int) bool { total := 0 for _, num := range nums { total += num } if total%2 != 0 { return false } target := total / 2 dp := make([]bool, target+1) dp[0] = true for _, num := range nums { for j := target; j >= num; j-- { dp[j] = dp[j] || dp[j-num] } } return dp[target] } func main() { nums := []int{1, 5, 11, 5} fmt.Println(canPartition(nums)) }
Attempts:
2 left
💡 Hint
Think about whether the total sum is even and if a subset sums to half the total.
✗ Incorrect
The total sum is 22, which is even. Half is 11. The subsets {11} and {1, 5, 5} both sum to 11. So the function returns true.
🧠 Conceptual
intermediateUnderstanding the DP Array in Partition Equal Subset Sum
In the dynamic programming solution for Partition Equal Subset Sum, what does the boolean array dp represent?
Attempts:
2 left
💡 Hint
Think about what true or false means for each sum j.
✗ Incorrect
dp[j] is true if there exists a subset of the processed elements that sums exactly to j, otherwise false.
🔧 Debug
advancedIdentify the Bug in Partition Equal Subset Sum Implementation
What error will the following Go code produce when run, and why?
DSA Go
package main import "fmt" func canPartition(nums []int) bool { total := 0 for _, num := range nums { total += num } if total%2 != 0 { return false } target := total / 2 dp := make([]bool, target) dp[0] = true for _, num := range nums { for j := target; j >= num; j-- { dp[j] = dp[j] || dp[j-num] } } return dp[target] } func main() { nums := []int{1, 5, 11, 5} fmt.Println(canPartition(nums)) }
Attempts:
2 left
💡 Hint
Check the size of dp and the indices used to access it.
✗ Incorrect
The dp slice is created with length target, but indices go up to target, causing an index out of range panic.
🚀 Application
advancedNumber of Items in DP Array for Partition Equal Subset Sum
Given an input array with total sum S, how many elements does the dp array have in the standard dynamic programming solution for Partition Equal Subset Sum?
Attempts:
2 left
💡 Hint
The dp array size depends on the target sum which is half of total sum.
✗ Incorrect
The dp array size is target + 1 where target = total sum / 2, so size is (S/2) + 1.
❓ Predict Output
expertOutput of Partition Equal Subset Sum with Negative Numbers
What is the output of this Go code that attempts to check partition equal subset sum with negative numbers included?
DSA Go
package main import "fmt" func canPartition(nums []int) bool { total := 0 for _, num := range nums { total += num } if total%2 != 0 { return false } target := total / 2 dp := make([]bool, target+1) dp[0] = true for _, num := range nums { for j := target; j >= num; j-- { dp[j] = dp[j] || dp[j-num] } } return dp[target] } func main() { nums := []int{1, -1, 2, 3, 5} fmt.Println(canPartition(nums)) }
Attempts:
2 left
💡 Hint
Consider how negative numbers affect indexing in dp array.
✗ Incorrect
Negative numbers cause out-of-bounds indices in dp[j-num], leading to runtime panic due to index out of range.
