Subsets & Combinations - Partition to K Equal Sum Subsets
Analyze the following Python code snippet for partitioning an array into
k equal sum subsets using backtracking with bitmask memoization. Identify the logical error that can cause incorrect results.
```python
def canPartitionKSubsets(nums, k):
total = sum(nums)
if total % k != 0:
return False
target = total // k
memo = {}
def backtrack(used, curr_sum, count):
if count == k:
return True
if curr_sum > target:
return False
if (used, curr_sum, count) in memo:
return memo[(used, curr_sum, count)]
for i in range(len(nums)):
if not (used & (1 << i)):
if curr_sum + nums[i] <= target:
if backtrack(used | (1 << i), curr_sum + nums[i], count):
memo[(used, curr_sum, count)] = True
return True
if curr_sum == target:
if backtrack(used, 0, count + 1):
memo[(used, curr_sum, count)] = True
return True
memo[(used, curr_sum, count)] = False
return False
return backtrack(0, 0, 0)
```
Which line contains the subtle bug?