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 C# code that checks if the array can be partitioned into two subsets with equal sum?
DSA C#
using System; class Program { static bool CanPartition(int[] nums) { int sum = 0; foreach (int num in nums) sum += num; if (sum % 2 != 0) return false; int target = sum / 2; bool[] dp = new bool[target + 1]; dp[0] = true; foreach (int num in nums) { for (int j = target; j >= num; j--) { dp[j] = dp[j] || dp[j - num]; } } return dp[target]; } static void Main() { int[] nums = {1, 5, 11, 5}; Console.WriteLine(CanPartition(nums)); } }
Attempts:
2 left
💡 Hint
Check if 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, and subsets {1,5,5} and {11} both sum to 11. The code correctly finds a subset with sum 11, so output is True.
🧠 Conceptual
intermediateUnderstanding the Partition Equal Subset Sum Problem
Which statement best describes the Partition Equal Subset Sum problem?
Attempts:
2 left
💡 Hint
Think about dividing 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.
🔧 Debug
advancedIdentify the Error in Subset Sum Implementation
What error will the following code produce when checking partition equal subset sum?
DSA C#
bool CanPartition(int[] nums) { int sum = 0; foreach (int num in nums) sum += num; if (sum % 2 != 0) return false; int target = sum / 2; bool[] dp = new bool[target]; dp[0] = true; foreach (int num in nums) { for (int j = target; j >= num; j--) { dp[j] = dp[j] || dp[j - num]; } } return dp[target]; }
Attempts:
2 left
💡 Hint
Check the size of the dp array and the indices accessed.
✗ Incorrect
The dp array is created with size target, but indices go up to target, causing IndexOutOfRangeException.
🚀 Application
advancedResult of Partition Check on Specific Input
Given the input array [2, 2, 3, 5], what will the Partition Equal Subset Sum function return?
DSA C#
bool CanPartition(int[] nums) { int sum = 0; foreach (int num in nums) sum += num; if (sum % 2 != 0) return false; int target = sum / 2; bool[] dp = new bool[target + 1]; dp[0] = true; foreach (int num in nums) { for (int j = target; j >= num; j--) { dp[j] = dp[j] || dp[j - num]; } } return dp[target]; } // Input: [2, 2, 3, 5]
Attempts:
2 left
💡 Hint
Calculate the total sum and check if half sum subset exists.
✗ Incorrect
Sum is 12, half is 6. Possible subsets sums: 2,4,5,7,8,10 but no subset sums exactly to 6. So returns False.
❓ Predict Output
expertOutput of Partition Equal Subset Sum with Negative Numbers
What is the output of the following code when the input array contains negative numbers?
DSA C#
using System; class Program { static bool CanPartition(int[] nums) { int sum = 0; foreach (int num in nums) sum += num; if (sum % 2 != 0) return false; int target = sum / 2; bool[] dp = new bool[target + 1]; dp[0] = true; foreach (int num in nums) { for (int j = target; j >= num && j >= 0; j--) { dp[j] = dp[j] || dp[j - num]; } } return dp[target]; } static void Main() { int[] nums = {1, -1, 2, 4}; Console.WriteLine(CanPartition(nums)); } }
Attempts:
2 left
💡 Hint
Consider how negative numbers affect array indexing.
✗ Incorrect
Negative numbers cause invalid indices in dp array access (j - num) leading to runtime exception.
