Count Partitions with Given Difference
Implement countPartitionsWithDiff
Given an array of non-negative integers and a target difference, count how many ways the array can be split into two groups — every element assigned to exactly one of the two — such that the first group's sum minus the second group's sum equals exactly that target difference.
If the two group sums are called S1 and S2, then S1 - S2 equals the target difference and S1 + S2 equals the array's total, which pins S2 down to one fixed value the moment the total and target are known. That turns the question into counting how many subsets of the array sum to exactly that fixed value — the same counting search as Count Subsets with Sum K, just with the target computed from the total and the desired difference first.
Example 1:
Input: nums = [2,2,1,3], diff = 2
Output: 3
Example 2:
Input: nums = [5,2,6,4], diff = 3
Output: 1
Example 3:
Input: nums = [1,1,1,1], diff = 0
Output: 6
+ 7 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ nums.length ≤ 20 - ●
0 ≤ nums[i] ≤ 100 - ●
0 ≤ diff ≤ 1000
nums =
[2, 2, 1, 3]
diff =
2