Facebook Pixel

Target Sum

Problem Statement

Given an integer array nums and an integer target, return the number of ways to assign + or - to each element such that the sum equals target.

Input Format

Line 1: Space-separated integers representing nums. Line 2: Target integer.

Output Format

Number of ways to achieve the target sum.

Examples

Example 1:

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

Ways: -1+1+1+1+1, +1-1+1+1+1, +1+1-1+1+1, +1+1+1-1+1, +1+1+1+1-1

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro