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 Java code that checks if the array can be partitioned into two subsets with equal sum?
DSA Java
public class Solution { public static boolean canPartition(int[] nums) { int sum = 0; for (int num : nums) sum += num; if (sum % 2 != 0) return false; int target = sum / 2; boolean[] dp = new boolean[target + 1]; dp[0] = true; for (int num : nums) { for (int j = target; j >= num; j--) { dp[j] = dp[j] || dp[j - num]; } } return dp[target]; } public static void main(String[] args) { int[] nums = {1, 5, 11, 5}; System.out.println(canPartition(nums)); } }
Attempts:
2 left
💡 Hint
Check if the total sum is even and then use dynamic programming to find subset with half sum.
✗ Incorrect
The total sum is 22 which is even. The code uses a boolean DP array to check if a subset sums to 11. Since {1,5,5} sums to 11, the output is true.
❓ Predict Output
intermediateOutput for No Equal Partition Possible
What will be the output of this Java code for the input array where partition into equal sum subsets is not possible?
DSA Java
public class Solution { public static boolean canPartition(int[] nums) { int sum = 0; for (int num : nums) sum += num; if (sum % 2 != 0) return false; int target = sum / 2; boolean[] dp = new boolean[target + 1]; dp[0] = true; for (int num : nums) { for (int j = target; j >= num; j--) { dp[j] = dp[j] || dp[j - num]; } } return dp[target]; } public static void main(String[] args) { int[] nums = {1, 2, 3, 5}; System.out.println(canPartition(nums)); } }
Attempts:
2 left
💡 Hint
Sum is 11 which is odd, so partition is impossible.
✗ Incorrect
The sum of the array is 11 which is odd, so it cannot be split into two equal subsets. The function returns false.
🧠 Conceptual
advancedUnderstanding the DP Array in Partition Equal Subset Sum
In the Partition Equal Subset Sum problem, what does the boolean DP array represent during the computation?
Attempts:
2 left
💡 Hint
Think about what true or false means for each sum index in dp.
✗ Incorrect
Each dp[i] is true if there exists a subset of the input numbers that sums exactly to i. This helps to check if half the total sum can be formed.
🔧 Debug
advancedIdentify the Bug in Partition Equal Subset Sum Implementation
What is the bug in the following Java code snippet for Partition Equal Subset Sum?
DSA Java
public boolean canPartition(int[] nums) { int sum = 0; for (int num : nums) sum += num; if (sum % 2 != 0) return false; int target = sum / 2; boolean[] dp = new boolean[target]; dp[0] = true; for (int num : nums) { for (int j = target; j >= num; j--) { dp[j] = dp[j] || dp[j - num]; } } return dp[target]; }
Attempts:
2 left
💡 Hint
Check array size and indexing carefully.
✗ Incorrect
The dp array must have size target + 1 to include index target. Using size target causes ArrayIndexOutOfBoundsException when accessing dp[target].
🚀 Application
expertNumber of Subsets with Sum Equal to Half Total
Given an array of positive integers, how can you modify the Partition Equal Subset Sum approach to find the number of subsets that sum to half the total sum?
Attempts:
2 left
💡 Hint
Counting subsets requires summing counts, not just true/false.
✗ Incorrect
To count subsets, use an integer dp array where dp[i] holds the number of subsets summing to i. For each num, update dp[j] += dp[j - num] for j from target down to num.
