Count Ordered Combination Sums with Negative Values
Updated
Problem statement
You are given an array candidates of distinct nonzero integers, an integer target, and a positive integer maxLength. Build a sequence by picking values from candidates one after another. Count the sequences that are nonempty, hold at most maxLength values, and add up to target. Return that count.
- You may reuse a candidate any number of times.
- Two sequences with the same values in different orders count separately.
- Sequences of different lengths count separately.
Function
countOrderedCombinationSumsWithNegatives(candidates: int[], target: int, maxLength: int) → intExamples
Example 1
candidates = [1,-1]target = 0maxLength = 2return = 2The valid nonempty sequences are [1,-1] and [-1,1]. The empty sequence is not counted.
Example 2
candidates = [2,-1]target = 3maxLength = 3return = 3The valid sequences are the three orderings of [2,2,-1].
Example 3
candidates = [-2,-1]target = -3maxLength = 3return = 3The valid sequences are [-2,-1], [-1,-2], and [-1,-1,-1].
Constraints
1 <= candidates.length <= 30.-100 <= candidates[i] <= 100andcandidates[i] != 0.- All values in
candidatesare distinct. -1000 <= target <= 1000.1 <= maxLength <= 20.- The correct answer fits a signed 32-bit integer.
More Pinterest Phone Screen questions
- Shortest Pin Path Through TopicsPHONE SCREEN · Seen Nov 2026
- Top K Pins with Insert and QueryPHONE SCREEN · Seen Nov 2026
- Pins Violation Log QueriesPHONE SCREEN · Seen Nov 2026
- Sparse Matrix Addition and MultiplicationPHONE SCREEN · Seen Nov 2026
- Minimum Bus Routes to a DestinationPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Delete and Compact a Forest SubtreePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Optimal Account BalancingPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Aggregate Matching IPv4 Addresses Across ResponsesPHONE SCREEN · Seen Oct 2026
- Escape Room LeaderboardPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Shortest Way to Form a Target StringPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Word Break IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Word Break IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEW
- Frog JumpZillow · PHONE SCREEN
- Predicate Expression Tree EvaluatorMongoDB · PHONE SCREEN
- Find Max Length of SubsequenceGoogle · PHONE SCREEN
- Longest Fibonacci-Like SubsequenceAdobe · PHONE SCREEN
- Longest Common Contiguous Browser HistoryRoblox · PHONE SCREEN
- Longest Common URL History RunDatadog · PHONE SCREEN
- Optimize Discount Selection Under a Point BudgetPalantir · OA
- Maximum Escape Game ScoreMicrosoft · OA