This approach involves using backtracking to attempt to build k
subsets, each with a sum equal to totalSum / k
. The decision tree can be pruned by sorting the numbers in decreasing order and tracking the sums of the current subset.
Time Complexity: O(kn), where n is the number of elements, due to exploring all partition possibilities.
Space Complexity: O(n + k) due to recursive call stack and subset sums array.
1#include <stdio.h>
2#include <stdbool.h>
3#include <stdlib.h>
4
5int compare(const void *a, const void *b) {
6 return *(int *)b - *(int *)a;
7}
8
9bool canPartition(int* nums, int numsSize, int k, int* subsetSums, int targetSubsetSum, int index) {
10 if (index == numsSize) return true;
11 for (int i = 0; i < k; i++) {
12 if (subsetSums[i] + nums[index] <= targetSubsetSum) {
13 subsetSums[i] += nums[index];
14 if (canPartition(nums, numsSize, k, subsetSums, targetSubsetSum, index + 1)) return true;
15 subsetSums[i] -= nums[index];
16 }
17 if (subsetSums[i] == 0) break; // Avoid trying same state again
18 }
19 return false;
20}
21
22bool canPartitionKSubsets(int* nums, int numsSize, int k) {
23 int totalSum = 0;
24 for (int i = 0; i < numsSize; i++) totalSum += nums[i];
25 if (totalSum % k != 0) return false;
26 int targetSubsetSum = totalSum / k;
27 qsort(nums, numsSize, sizeof(int), compare);
28 int* subsetSums = (int*)calloc(k, sizeof(int));
29 bool result = canPartition(nums, numsSize, k, subsetSums, targetSubsetSum, 0);
30 free(subsetSums);
31 return result;
32}
33
34int main() {
35 int nums[] = {4, 3, 2, 3, 5, 2, 1};
36 int k = 4;
37 int numsSize = sizeof(nums) / sizeof(nums[0]);
38 printf("%s\n", canPartitionKSubsets(nums, numsSize, k) ? "true" : "false");
39 return 0;
40}
This solution begins by calculating the total sum of elements and checks early if this is divisible by k
. It uses sorting to try larger numbers first, which helps in quicker pruning of impossible branches. The recursive function tries to add each number to the current subset sum and backtracks when necessary.
A less intuitive but potentially powerful technique involves using bitmasking to represent subsets and top-down dynamic programming to evaluate the feasibility of partitioning. This uses state compression where each state represents a set of elements already partitioned; 1 means used, 0 means unused.
Time Complexity: O(n * 2n), determined by the states and transitions across the full bitmask.
Space Complexity: O(2n), which is used to store DP states for all subset configurations.
1#include <stdio.h>
2#include <stdbool.h>
3#include <string.h>
4
5int dp[1 << 16], maskSum[1 << 16];
6
7bool canPartitionKSubsets(int* nums, int numsSize, int k) {
8 int totalSum = 0;
9 for (int i = 0; i < numsSize; ++i) totalSum += nums[i];
10 if (totalSum % k != 0) return false;
11 int targetSubsetSum = totalSum / k;
12
13 memset(dp, -1, sizeof(dp));
14 int allMask = (1 << numsSize) - 1;
15 dp[0] = 0;
16
17 // Traverse all states
18 for (int mask = 0; mask <= allMask; ++mask) {
19 if (dp[mask] == -1) continue;
20 for (int j = 0; j < numsSize; ++j) {
21 if (!(mask & (1 << j)) && dp[mask] + nums[j] <= targetSubsetSum) {
22 int newMask = mask | (1 << j);
23 dp[newMask] = (dp[mask] + nums[j]) % targetSubsetSum;
24 }
25 }
26 }
27
28 return dp[allMask] == 0;
29}
30
31int main() {
32 int nums[] = {4, 3, 2, 3, 5, 2, 1};
33 int k = 4;
34 int numsSize = sizeof(nums) / sizeof(nums[0]);
35 printf("%s\n", canPartitionKSubsets(nums, numsSize, k) ? "true" : "false");
36 return 0;
37}
This approach encodes subsets using bitmasking where the nth bit represents the nth element in the nums array. Dynamic programming is used to store the remainder when each mask's subset sum is divided by targetSubsetSum. A successful partition is found when the full mask covers all elements with a remainder of 0.