1004. Count all subsequences with sum K

Given an array nums and an integer k.Return the number of non-empty subsequences of nums such that the sum of all elements in the subsequence is equal to k.

Example 1:

Input : nums = [4, 9, 2, 5, 1] , k = 10

Output : 2

Explanation : The possible subsets with sum k are [9, 1] , [4, 5, 1].

Example 2:

Input : nums = [4, 2, 10, 5, 1, 3] , k = 5

Output : 3

Explanation : The possible subsets with sum k are [4, 1] , [2, 3] , [5].

Now Your Turn!

Pick the correct output for the given input

Input : nums = [1, 10, 4, 5] , k = 16

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= nums.length <= 20
  • 1 <= nums[i] <= 100
  • 1 <= k <= 2000

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution{
public:
int countSubsequenceWithTargetSum(vector<int>& nums, int k){
//your code goes here
}
};
Test Case

Input:

K
Nums