Challenge - 5 Problems
Partition 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 JavaScript code that checks if an array can be partitioned into two subsets with equal sum?
DSA Javascript
function canPartition(nums) {
const total = nums.reduce((a, b) => a + b, 0);
if (total % 2 !== 0) return false;
const target = total / 2;
const dp = new Array(target + 1).fill(false);
dp[0] = true;
for (const num of nums) {
for (let j = target; j >= num; j--) {
dp[j] = dp[j] || dp[j - num];
}
}
return dp[target];
}
console.log(canPartition([1, 5, 11, 5]));Attempts:
2 left
💡 Hint
Check if the total sum is even and then use dynamic programming to find subset sums.
✗ Incorrect
The total sum is 22, which is even. Target is 11. The DP uses a backward loop to mark achievable sums without reusing elements. [1,5,5] sums to 11, so dp[11] becomes true. The function returns true.
🧠 Conceptual
intermediateUnderstanding the Partition Equal Subset Sum Problem
Which statement best describes the Partition Equal Subset Sum problem?
Attempts:
2 left
💡 Hint
Focus on splitting the array into two parts with the same total.
✗ Incorrect
The problem asks if the array can be divided into two subsets where the sum of elements in both subsets is equal.
❓ Predict Output
advancedOutput of Partition Check with No Possible Partition
What is the output of this code snippet?
DSA Javascript
function canPartition(nums) {
const total = nums.reduce((a, b) => a + b, 0);
if (total % 2 !== 0) return false;
const target = total / 2;
const dp = new Array(target + 1).fill(false);
dp[0] = true;
for (const num of nums) {
for (let j = target; j >= num; j--) {
dp[j] = dp[j] || dp[j - num];
}
}
return dp[target];
}
console.log(canPartition([1, 2, 3, 5]));Attempts:
2 left
💡 Hint
Check if the total sum is even and if subsets can form half the sum.
✗ Incorrect
The total sum is 11, which is odd, so the function returns false immediately.
🔧 Debug
advancedIdentify the Error in Partition Function
What is the output of this code snippet?
DSA Javascript
function canPartition(nums) {
const total = nums.reduce((a, b) => a + b, 0);
if (total % 2 !== 0) return false;
const target = total / 2;
const dp = new Array(target + 1).fill(false);
dp[0] = true;
for (const num of nums) {
for (let j = num; j <= target; j++) {
dp[j] = dp[j] || dp[j - num];
}
}
return dp[target];
}
console.log(canPartition([1, 3]));Attempts:
2 left
💡 Hint
Check the direction of the inner loop and how dp array is updated.
✗ Incorrect
Total sum is 4 (even), target=2. The inner loop runs forward (j from num to target), so when processing num=1, dp[1] is set to true, then dp[2] |= dp[1] (updated value), setting dp[2]=true. This incorrectly simulates using 1 twice. Actually, no subset sums to 2. Returns true incorrectly.
🚀 Application
expertNumber of Subsets with Equal Sum Partition
Given an array, how many subsets exist that sum to half of the total sum if the array can be partitioned equally? Consider the array [2, 2, 3, 5].
Attempts:
2 left
💡 Hint
Calculate total sum, then count subsets with sum equal to half using dynamic programming.
✗ Incorrect
Total sum is 12 (even), half is 6. No subsets sum to 6: singles <6, pairs: 2+2=4, 2+3=5, 2+5=7, 3+5=8; triples: 2+2+3=7, 2+2+5=9, 2+3+5=10; quad=12. So 0 subsets.
