783. Count All Subsequences with Given Sum (Modulo 10^9+7)

MediumRecursionRecursion

Given an integer array nums and an integer target, return the number of subsequences (not necessarily contiguous) whose elements sum to exactly target, taken modulo 10^9 + 7. The empty subsequence has sum 0. Elements may be negative. Implement recursively.

Input: An integer array and an integer target: "[a1,...], target".

Output: Return an integer count mod 10^9 + 7.

Examples

Example 1
Input: [1,2,3], 3
Output: 2
Explanation: Subsequences {3} and {1,2}.
Example 2
Input: [1,1,1,1,1], 3
Output: 10
Explanation: C(5,3) = 10 ways to pick 3 ones.
Example 3
Input: [1,2,3], 7
Output: 0
Explanation: Max sum is 6 < 7.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →