Loading
Loading
An auditor is reconstructing a ledger. She knows the size of every entry but the sign column was lost: each entry was either a credit (+) or a debit (−). She also knows what the entries netted to.
Given the entry sizes and the net total, return how many ways of assigning a sign to every entry produce exactly that net. Entries are distinguished by position, so two equal entries with swapped signs are two different assignments, and an entry of 0 can take either sign.
values = [2, 1, 1, 2], target = 2+2 −1 −1 +2 = 2assignment 1+2 +1 +1 −2 = 2assignment 2−2 +1 +1 +2 = 2assignment 3plus side must sum to (6 + 2) / 2 = 4so count subsets that sum to 43Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(n × total) time · O(total) space
Input. Run your code to watch it work on this input, one line at a time.
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
+2 −1 −1 +2, +2 +1 +1 −2 and −2 +1 +1 +2.
Example 2
4 ± 6 is always even, so an odd net is impossible.