Bird
Raised Fist0
DSA Cprogramming~20 mins

Partition Equal Subset Sum in DSA C - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Partition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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;
}
ACompilation error
Bfalse
CRuntime error
Dtrue
Attempts:
2 left
💡 Hint
Check if the total sum is even and then use dynamic programming to find subset with sum equal to half.
🧠 Conceptual
intermediate
1: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?
ABecause two equal integers must sum to an even number
BBecause odd sums can be split into two equal parts
CBecause the array length must be even
DBecause the subsets must have the same number of elements
Attempts:
2 left
💡 Hint
Think about what happens if the sum is odd and you try to split it equally.
🔧 Debug
advanced
2: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;
}
ASum calculation is incorrect
BLoop indices are out of bounds causing segmentation fault
Cdp array initialization misses last element, causing undefined behavior
DNo error, code runs correctly
Attempts:
2 left
💡 Hint
Check the loop that initializes the dp array.
🚀 Application
advanced
2:00remaining
Minimum subset sum difference using partition logic
Given an array, which approach helps find the minimum difference between sums of two subsets?
AUse partition equal subset sum logic to find all achievable sums up to half total and pick closest to half
BSort array and split into two halves directly
CSum all elements and divide by length
DUse greedy approach to assign elements alternately to subsets
Attempts:
2 left
💡 Hint
Think about how subset sums relate to partitioning.
❓ Predict Output
expert
2: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;
}
Afalse
BRuntime error due to negative index in dp array
CCompilation error
Dtrue
Attempts:
2 left
💡 Hint
Consider how negative numbers affect array indexing.

Practice

(1/5)
1. What does the Partition Equal Subset Sum problem ask you to find?
easy
A. If the array contains any duplicate elements
B. If the array is sorted in ascending order
C. If the array can be split into two subsets with equal sums
D. If the array contains only positive numbers

Solution

  1. Step 1: Understand the problem statement

    The problem asks whether the given array can be divided into two parts where the sum of elements in both parts is the same.
  2. Step 2: Identify the key requirement

    We need to check if there exists a subset whose sum is half of the total sum of the array.
  3. Final Answer:

    If the array can be split into two subsets with equal sums -> Option C
  4. Quick Check:

    Partition Equal Subset Sum = Split array equally [OK]
Hint: Check if total sum is even first to proceed [OK]
Common Mistakes:
  • Thinking the problem is about sorting
  • Checking for duplicates instead of sums
  • Assuming all numbers must be positive
2. Which of the following is the correct way to declare a boolean array in C for dynamic programming in Partition Equal Subset Sum?
easy
A. boolean dp[sum+1];
B. int dp[sum+1];
C. bool dp = new bool[sum+1];
D. bool dp[sum+1];

Solution

  1. Step 1: Identify correct C syntax for boolean arrays

    In C, bool is available via stdbool.h and arrays are declared as bool dp[size];.
  2. Step 2: Check options for correctness

    bool dp[sum+1]; uses correct syntax. int dp[sum+1]; uses int, which is not boolean. bool dp = new bool[sum+1]; uses Java/C++ style dynamic allocation, not valid in C. boolean dp[sum+1]; uses 'boolean' which is not a C keyword.
  3. Final Answer:

    bool dp[sum+1]; -> Option D
  4. Quick Check:

    Use bool dp[size]; in C [OK]
Hint: Use stdbool.h and declare bool arrays with bool dp[size]; [OK]
Common Mistakes:
  • Using 'boolean' instead of 'bool' in C
  • Trying Java-style dynamic allocation in C
  • Using int instead of bool for DP array
3. What will be the output of the following code snippet for array nums = {1, 5, 11, 5}?
#include 
#include 

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};
    printf("%d\n", canPartition(nums, 4));
    return 0;
}
medium
A. 1
B. 0
C. Compilation error
D. Runtime error

Solution

  1. Step 1: Calculate total sum and check parity

    Sum = 1 + 5 + 11 + 5 = 22, which is even, so proceed.
  2. Step 2: Use DP to check subset sum equals 11

    The DP array tracks sums achievable. The subset {11} and {1,5,5} both sum to 11, so dp[11] becomes true.
  3. Final Answer:

    1 -> Option A
  4. Quick Check:

    Subset sum exists = 1 [OK]
Hint: Check if half sum subset exists using DP [OK]
Common Mistakes:
  • Not checking if total sum is even
  • Incorrect DP initialization
  • Looping forward instead of backward in DP
4. Identify the bug in this Partition Equal Subset Sum code snippet:
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];
}
medium
A. Loop initializing dp should run till target inclusive
B. The sum calculation is incorrect
C. The inner loop should run forward, not backward
D. dp[0] should be false initially

Solution

  1. Step 1: Check dp array initialization loop

    The loop runs from 0 to target-1, missing dp[target] initialization, which should be set to false.
  2. Step 2: Correct the loop to include target index

    Change loop condition to i <= target to initialize all dp elements properly.
  3. Final Answer:

    Loop initializing dp should run till target inclusive -> Option A
  4. Quick Check:

    Initialize dp array fully including dp[target] [OK]
Hint: Initialize dp array fully including last index [OK]
Common Mistakes:
  • Forgetting to initialize dp[target]
  • Running inner loop forward causing wrong results
  • Setting dp[0] to false instead of true
5. Given an array nums = {2, 2, 3, 5}, which of the following modifications will correctly determine if it can be partitioned into two subsets with equal sum using dynamic programming?
hard
A. Change the DP array to track counts of subsets instead of booleans
B. Use a 2D DP array where dp[i][j] means if sum j is possible with first i elements
C. Sort the array first and then apply the existing 1D DP approach
D. Only check if the largest element is less than half the total sum

Solution

  1. Step 1: Understand limitations of 1D DP for some cases

    1D DP works but 2D DP is clearer and handles subset sums by tracking elements used.
  2. Step 2: Use 2D DP where dp[i][j] shows if sum j is possible using first i elements

    This approach explicitly considers each element and sum, making it easier to debug and understand.
  3. Step 3: Sorting or counting subsets is not necessary or sufficient

    Sorting doesn't affect subset sums. Counting subsets is more complex and not required for yes/no answer.
  4. Final Answer:

    Use a 2D DP array where dp[i][j] means if sum j is possible with first i elements -> Option B
  5. Quick Check:

    2D DP tracks sums per element [OK]
Hint: Use 2D DP for clear subset sum tracking [OK]
Common Mistakes:
  • Sorting array expecting it to solve partition
  • Counting subsets instead of checking existence
  • Only checking largest element against half sum
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard