Skip to content
Decoder

Target Sum

Medium

Problem

You are given an integer array nums and an integer target. Put either a `+` or a `-` sign in front of every number, then add them all up. Return the number of different sign assignments that make the total equal target.

Example 1

Input
nums = [1, 1, 1, 1, 1], target = 3
Output
5
Why
one of the five 1s gets a minus

Example 2

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

Example 3

Input
nums = [0, 0, 0], target = 0
Output
8
Why
each 0 can be + or -

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.