Skip to content
Onsite

Count Subsets with Given Sum

MediumP2Coding

Return the number of subsets of nums whose elements add up to x. Subsets are chosen by position, so equal values at different positions count as different subsets. Zeros are allowed, and the empty subset (sum 0) counts when x = 0.

Example 1

Input
nums = [1,2,3,3], x = 6
Output
3
Why
Subsets: {1,2,3} using either 3, and {3,3}.

Example 2

Input
nums = [1,1,1,1], x = 2
Output
6

Example 3

Input
nums = [], x = 0
Output
1

Constraints

  • 0 <= nums.length <= 50
  • 0 <= nums[i] <= 100
  • 0 <= x <= 5000
  • The answer fits in a 53-bit integer.

Hints

Run checks the 3 visible tests, Submit all 11.