public bool CanPartitionKSubsets(int[] nums, int k) {

```
if (nums.Length == 0 || k <= 0)
{
return false;
}
int sum = 0;
foreach (int num in nums)
{
sum += num;
}
if (sum % k != 0)
{
return false;
}
int target = sum / k;
bool[] res = new bool[target + 1];
res[0] = true;
for (int i = 0; i < nums.Length; i++)
{
for (int j = target; j >= nums[i]; j--)
{
res[j] = res[j] || res[j - nums[i]];
}
}
return res[target];
}
```