Skip to the code
cf.
+25 XP

#488 Signed Nudges to a Target

medium

A layout tool applies a list of nudges to an element, in order. Each nudge has a size in nudges, and for each one you choose its direction: + moves the element right by that size and - moves it left. The element starts at position 0.

Write countSignedNudges(nudges, target) that returns how many different choices of signs leave the element exactly at target. A nudge of size 0 still has two signs, and both count.

Example 1

Input: nudges = [1,1,1,1], target = 2
Output: 4

Exactly one of the four nudges must be negative, and there are 4 ways to pick it.

Example 2

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

Only -2 + 3 gives 1.

Constraints

  • 1 <= nudges.length <= 20
  • 0 <= nudges[i] <= 1000
  • -1000 <= target <= 1000
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).