Count the plus and minus sign assignments that make an array sum to a target

Quick Overview

Count the ways to place a plus or minus sign before each number in an array so that the resulting expression equals a target value. Tests dynamic programming over sign choices, along with handling zeros and targets that cannot be reached.

Count the plus and minus sign assignments that make an array sum to a target

Company: Mercari

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an integer array `nums` and an integer `target`. You want to build an expression out of `nums` by adding one of the symbols `'+'` and `'-'` before each integer in `nums` and then concatenating all the integers. For example, if `nums = [2, 1]`, you can add a `'+'` before `2` and a `'-'` before `1` and concatenate them to build the expression `"+2-1"`. Return the number of different expressions you can build that evaluate to `target`. ### Function Signature ```python def find_target_sum_ways(nums: list[int], target: int) -> int: ``` ### Rules - The integers keep their order. Only the sign in front of each one is chosen. - Each way of assigning signs to the positions is one expression, so there are `2^n` expressions in total for `n` numbers. For example, `+0` and `-0` at the same position give two different expressions with the same value. ### Constraints - `1 <= len(nums) <= 20` - `0 <= nums[i] <= 1000` - `0 <= sum(nums) <= 1000` - `-1000 <= target <= 1000` ### Examples ```text Input: nums = [1, 1, 1, 1, 1], target = 3 Output: 5 ``` Exactly one of the five 1s must get a minus sign: `-1+1+1+1+1`, `+1-1+1+1+1`, `+1+1-1+1+1`, `+1+1+1-1+1` and `+1+1+1+1-1`. ```text Input: nums = [1], target = 1 Output: 1 ``` Only the expression `"+1"` evaluates to 1. ```text Input: nums = [0, 0, 1], target = 1 Output: 4 ``` The 1 must be positive, and each 0 can take either sign without changing the value. The four expressions are `+0+0+1`, `+0-0+1`, `-0+0+1` and `-0-0+1`.

Overview: Count the ways to place a plus or minus sign before each number in an array so that the resulting expression equals a target value. Tests dynamic programming over sign choices, along with handling zeros and targets that cannot be reached.

Mercari logo
Mercari
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an integer array nums and an integer target.

You want to build an expression out of nums by adding one of the symbols '+' and '-' before each integer in nums and then concatenating all the integers. For example, if nums = [2, 1], you can add a '+' before 2 and a '-' before 1 and concatenate them to build the expression "+2-1".

Return the number of different expressions you can build that evaluate to target.

Function Signature

def find_target_sum_ways(nums: list[int], target: int) -> int:

Rules

  • The integers keep their order. Only the sign in front of each one is chosen.
  • Each way of assigning signs to the positions is one expression, so there are 2^n expressions in total for n numbers. For example, +0 and -0 at the same position give two different expressions with the same value.

Constraints

  • 1 <= len(nums) <= 20
  • 0 <= nums[i] <= 1000
  • 0 <= sum(nums) <= 1000
  • -1000 <= target <= 1000

Examples

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

Exactly one of the five 1s must get a minus sign: -1+1+1+1+1, +1-1+1+1+1, +1+1-1+1+1, +1+1+1-1+1 and +1+1+1+1-1.

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

Only the expression "+1" evaluates to 1.

Input: nums = [0, 0, 1], target = 1
Output: 4

The 1 must be positive, and each 0 can take either sign without changing the value. The four expressions are +0+0+1, +0-0+1, -0+0+1 and -0-0+1.