Challenge - 5 Problems
Partition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of subset sum check for equal partition
What is the output of the following C code that checks if an array can be partitioned into two subsets with equal sum?
DSA C
#include <stdio.h> #include <stdbool.h> bool canPartition(int* nums, int numsSize) { int sum = 0; for (int i = 0; i < numsSize; i++) { sum += nums[i]; } if (sum % 2 != 0) return false; int target = sum / 2; bool dp[target + 1]; for (int i = 0; i <= target; i++) dp[i] = false; dp[0] = true; for (int i = 0; i < numsSize; i++) { for (int j = target; j >= nums[i]; j--) { dp[j] = dp[j] || dp[j - nums[i]]; } } return dp[target]; } int main() { int nums[] = {1, 5, 11, 5}; int size = sizeof(nums) / sizeof(nums[0]); if (canPartition(nums, size)) printf("true\n"); else printf("false\n"); return 0; }
Attempts:
2 left
💡 Hint
Check if the total sum is even and then use dynamic programming to find subset with sum equal to half.
✗ Incorrect
The array sums to 22, which is even. The subset {11, 5} sums to 16, but the correct subset is {11, 5} and {1, 5, 5} both sum to 11. The code correctly identifies this and returns true.
🧠 Conceptual
intermediate1:30remaining
Why must the total sum be even for partition?
Why is it necessary for the total sum of the array elements to be even to partition it into two subsets with equal sum?
Attempts:
2 left
💡 Hint
Think about what happens if the sum is odd and you try to split it equally.
✗ Incorrect
Two equal integers sum to an even number. If the total sum is odd, it cannot be split into two equal integer sums.
🔧 Debug
advanced2:00remaining
Identify the error in subset sum implementation
What error does the following code produce when trying to check partition equal subset sum?
DSA C
#include <stdio.h> #include <stdbool.h> bool canPartition(int* nums, int numsSize) { int sum = 0; for (int i = 0; i < numsSize; i++) { sum += nums[i]; } if (sum % 2 != 0) return false; int target = sum / 2; bool dp[target + 1]; for (int i = 0; i < target; i++) dp[i] = false; dp[0] = true; for (int i = 0; i < numsSize; i++) { for (int j = target; j >= nums[i]; j--) { dp[j] = dp[j] || dp[j - nums[i]]; } } return dp[target]; } int main() { int nums[] = {1, 2, 3, 5}; int size = sizeof(nums) / sizeof(nums[0]); if (canPartition(nums, size)) printf("true\n"); else printf("false\n"); return 0; }
Attempts:
2 left
💡 Hint
Check the loop that initializes the dp array.
✗ Incorrect
The dp array is of size target+1 but the loop initializes only up to target-1, leaving dp[target] uninitialized which can cause wrong results or undefined behavior.
🚀 Application
advanced2:00remaining
Minimum subset sum difference using partition logic
Given an array, which approach helps find the minimum difference between sums of two subsets?
Attempts:
2 left
💡 Hint
Think about how subset sums relate to partitioning.
✗ Incorrect
By finding all subset sums up to half the total sum, we can find the subset sum closest to half, minimizing the difference between two subsets.
❓ Predict Output
expert2:30remaining
Output of partition check with negative numbers
What is the output of this code that attempts to check partition equal subset sum with negative numbers included?
DSA C
#include <stdio.h> #include <stdbool.h> bool canPartition(int* nums, int numsSize) { int sum = 0; for (int i = 0; i < numsSize; i++) { sum += nums[i]; } if (sum % 2 != 0) return false; int target = sum / 2; bool dp[target + 1]; for (int i = 0; i <= target; i++) dp[i] = false; dp[0] = true; for (int i = 0; i < numsSize; i++) { for (int j = target; j >= nums[i]; j--) { dp[j] = dp[j] || dp[j - nums[i]]; } } return dp[target]; } int main() { int nums[] = {1, 2, -1, 4}; int size = sizeof(nums) / sizeof(nums[0]); if (canPartition(nums, size)) printf("true\n"); else printf("false\n"); return 0; }
Attempts:
2 left
💡 Hint
Consider how negative numbers affect array indexing.
✗ Incorrect
The code uses dp[j - nums[i]] where nums[i] can be negative, causing negative index access which leads to runtime error.