Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Target Sum Ways

Hard Acceptance 64.00% Points 40.00

The Target Sum Ways problem requires finding the number of different ways to assign either a '+' or '-' sign to each element in a given integer array such that the resulting expression equals a given target value. Every element must be used exactly once, and the order of elements cannot be changed. The task is to return the total number of possible ways to reach the target.

Examples
Example 1

Example 1

Input: nums = [1,1,1,1,1], target = 3

Output: 5

Explanation: There are five different ways to assign '+' and '-' signs to make the sum equal to 3.

Example 2

Example 2

Input: nums = [1], target = 1

Output: 1

Explanation: Only one way exists: +1 equals 1.

Example 3

Example 3

Input: nums = [2,1], target = 1

Output: 1

Explanation: One valid way is +2 -1 = 1. No other sign combination produces the target.

Hints
Hint 1
N/A
Constraints
  • 1 = nums.length = 20
  • 0 = nums[i] = 1000
  • -1000 = target = 1000
Companies
Amazon Microsoft Apple Samsung Visa + 6 more
Topics
Recursion
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions