0
0
DSA Cprogramming~20 mins

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

Choose your learning style9 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.