Count of Subsets with Given Difference

Given an array of positive integers and a difference d, return the number of ways to partition the array into two subsets such that the difference of their sums equals d. This is a classic counting transformation from partition DP to subset-sum counting.

Input Format

nums = array of non-negative integers, d = required difference

Output Format

number of valid partitions

Constraints

  • 1 <= nums.length <= 100; 0 <= nums[i] <= 1000; 0 <= d <= 1000

Examples

Example 1:

Input:

nums = [1,1,2,3]
d = 1

Output:

3

Explanation:

There are 3 partitions whose subset sums differ by 1.

Example 2:

Input:

nums = [1,2,7,1]
d = 9

Output:

2

Explanation:

The target subset-sum reduction yields 2 valid subsets.

Loading...
Count of Subsets with Given Difference