Skip to the code
cf.
+10 XP

#289 Signed Budget Adjustments

easy

A finance dashboard lists budget adjustments in amounts. Each adjustment must be applied either as a credit (+) or as a debit (-).

Write countSignChoices(amounts, target) that returns how many ways there are to give every amount a sign so that the signed amounts add up to target.

Two ways are different if at least one position has a different sign, even when the amounts are equal (so [0] with target 0 has 2 ways).

Example 1

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

Exactly one of the five 1s must be negative, and there are 5 ways to choose which.

Example 2

Input: amounts = [2,3], target = 1
Output: 1

Only -2 + 3 gives 1.

Constraints

  • 1 <= amounts.length <= 15
  • 0 <= amounts[i] <= 100
  • -1500 <= target <= 1500
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).