ORIGINAL CODING QUESTION
Expense picks that hit the budget
Count the sets of expense positions whose amounts add up exactly to the target. Two selections differ if they use different positions, even when the amounts match. The empty selection counts when the target is 0.
Original exercise · medium. Editorial role relevance does not imply a company has asked this question.
Input and output
n, then n non-negative amounts, then the target.
Constraints
1 ≤ n ≤ 12; 0 ≤ amount ≤ 50; 0 ≤ target ≤ 600.
Example 1
Input
4 1 2 3 3 3
Output
3
Either 3 alone (two positions) or 1 + 2.
Example 2
Input
3 0 0 5 0
Output
4
Any combination of the two zero amounts works: four selections.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
Each position is either included or not.
Hint 2
Let ways[s] count selections so far that sum to s.
Hint 3
For each amount update ways from high sums to low; a zero amount doubles every count.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →