Challenge - 5 Problems
Partition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of subset sum check for equal partition
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++
#include <iostream> #include <vector> using namespace std; bool canPartition(vector<int>& nums) { int sum = 0; for (int num : nums) sum += num; if (sum % 2 != 0) return false; int target = sum / 2; vector<bool> dp(target + 1, false); dp[0] = true; for (int num : nums) { for (int j = target; j >= num; --j) { dp[j] = dp[j] || dp[j - num]; } } return dp[target]; } int main() { vector<int> nums = {1, 5, 11, 5}; cout << (canPartition(nums) ? "true" : "false") << endl; return 0; }
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. Subsets {1,5,5} and {11} both sum to 11, so partition is possible. Output is true.
🧠 Conceptual
intermediateUnderstanding the base case in Partition Equal Subset Sum
In the dynamic programming solution for Partition Equal Subset Sum, why do we initialize dp[0] = true?
Attempts:
2 left
💡 Hint
Think about what sum zero means for subsets.
✗ Incorrect
dp[0] = true means that we can always form a subset with sum zero by choosing no elements (empty subset). This is the base case for building up solutions.
🔧 Debug
advancedIdentify the error in this Partition Equal Subset Sum code snippet
What error will this code produce when run?
DSA C++
#include <vector> using namespace std; bool canPartition(vector<int>& nums) { int sum = 0; for (int num : nums) sum += num; if (sum % 2 != 0) return false; int target = sum / 2; vector<bool> dp(target, false); 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 the size of dp vector and the indices accessed.
✗ Incorrect
The dp vector is initialized with size 'target', but indices go up to 'target'. Accessing dp[target] causes out_of_range error.
🚀 Application
advancedNumber of subsets with sum equal to half total
Given the array {2, 2, 3, 5}, how many subsets sum to half of the total sum?
Attempts:
2 left
💡 Hint
Calculate total sum, then find subsets summing to half.
✗ Incorrect
Total sum is 12, half is 6. No subset sums to 6 (e.g., 2+2+3=7, 2+3=5, 2+2=4, etc.). Thus, 0 subsets.
❓ Predict Output
expertOutput of recursive Partition Equal Subset Sum with memoization
What is the output of this C++ code that uses recursion and memoization to check partition?
DSA C++
#include <iostream> #include <vector> using namespace std; bool dfs(int idx, int target, vector<int>& nums, vector<vector<int>>& memo) { if (target == 0) return true; if (idx >= nums.size() || target < 0) return false; if (memo[idx][target] != -1) return memo[idx][target]; bool include = dfs(idx + 1, target - nums[idx], nums, memo); bool exclude = dfs(idx + 1, target, nums, memo); memo[idx][target] = include || exclude; return memo[idx][target]; } bool canPartition(vector<int>& nums) { int sum = 0; for (int num : nums) sum += num; if (sum % 2 != 0) return false; int target = sum / 2; vector<vector<int>> memo(nums.size(), vector<int>(target + 1, -1)); return dfs(0, target, nums, memo); } int main() { vector<int> nums = {1, 2, 3, 5}; cout << (canPartition(nums) ? "true" : "false") << endl; return 0; }
Attempts:
2 left
💡 Hint
Check if the sum is even and if a subset sums to half.
✗ Incorrect
Sum is 11, which is odd, so partition is impossible. The code returns false.
