Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Target Sum Ways

Hard Acceptance 64% Points 40

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.

Constraints
  • 1 = nums.length = 20
  • 0 = nums[i] = 1000
  • -1000 = target = 1000
Topics
Recursion
Companies
Amazon Microsoft Apple Samsung Visa Walmart Adobe Atlassian Wayfair Media.net BNY Mellon
Added 19 Jul 2026 64% acceptance
Ln 1, Col 1Sign in to save your code
1 1 1 1 1
3
5