Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Target Sum

Medium
dynamic-programmingknapsackbacktracking

Assign + or - to every element of nums; count the assignments whose total equals target.

Input

  • Line 1: space-separated non-negative integers
  • Line 2: integer target

Output: the number of ways.

Example 1
Input:
1 1 1 1 1
3
Output:
5
Example 2
Input:
1
1
Output:
1

Constraints

  • 1 ≤ nums.length ≤ 20
  • 0 ≤ nums[i] ≤ 1000; Σnums ≤ 1000
  • -1000 ≤ target ≤ 1000
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.