Count ordered ways to reach a target sum with unlimited reuse of distinct numbers

Quick Overview

Count how many ordered sequences built from a list of distinct positive integers, with unlimited reuse, add up exactly to a target, where different orders of the same numbers count separately. It tests one-dimensional dynamic programming and how the loop order decides whether orderings are counted.

Count ordered ways to reach a target sum with unlimited reuse of distinct numbers

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a list of distinct integers `nums` and a target integer `target`. Determine how many different ordered sequences of numbers from `nums` (with repetition allowed) have a sum exactly equal to `target`. Different permutations of the same multiset of numbers count as separate solutions. Return the total count of such sequences. The count is guaranteed to fit in a 32-bit integer. ### Function Signature ```python def count_ordered_sequences(nums: list[int], target: int) -> int: ``` ### Rules - Each number in `nums` may be used any number of times in a sequence, including zero times. - Order matters: `(1, 2, 1)` and `(2, 1, 1)` are two different sequences. - Return `0` when no sequence reaches `target`. ### Constraints - `1 <= nums.length <= 150` - `1 <= nums[i] <= 750` - `1 <= target <= 750` - All integers in `nums` are unique. ### Examples ```text Input: nums = [1, 2], target = 4 Output: 5 ``` The five ordered sequences are `(1, 1, 1, 1)`, `(1, 1, 2)`, `(1, 2, 1)`, `(2, 1, 1)` and `(2, 2)`. Sequences with the same numbers in a different order, such as `(1, 2, 1)` and `(2, 1, 1)`, count separately. ```text Input: nums = [3, 5, 7], target = 10 Output: 3 ``` The sequences are `(3, 7)`, `(7, 3)` and `(5, 5)`. No sequence of only 3s works, since 9 and 12 skip over 10. ```text Input: nums = [9], target = 3 Output: 0 ``` The only available number is larger than the target, so no sequence sums to 3.

Overview: Count how many ordered sequences built from a list of distinct positive integers, with unlimited reuse, add up exactly to a target, where different orders of the same numbers count separately. It tests one-dimensional dynamic programming and how the loop order decides whether orderings are counted.

General logo
General
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a list of distinct integers nums and a target integer target. Determine how many different ordered sequences of numbers from nums (with repetition allowed) have a sum exactly equal to target. Different permutations of the same multiset of numbers count as separate solutions.

Return the total count of such sequences. The count is guaranteed to fit in a 32-bit integer.

Function Signature

def count_ordered_sequences(nums: list[int], target: int) -> int:

Rules

  • Each number in nums may be used any number of times in a sequence, including zero times.
  • Order matters: (1, 2, 1) and (2, 1, 1) are two different sequences.
  • Return 0 when no sequence reaches target .

Constraints

  • 1 <= nums.length <= 150
  • 1 <= nums[i] <= 750
  • 1 <= target <= 750
  • All integers in nums are unique.

Examples

Input: nums = [1, 2], target = 4
Output: 5

The five ordered sequences are (1, 1, 1, 1), (1, 1, 2), (1, 2, 1), (2, 1, 1) and (2, 2). Sequences with the same numbers in a different order, such as (1, 2, 1) and (2, 1, 1), count separately.

Input: nums = [3, 5, 7], target = 10
Output: 3

The sequences are (3, 7), (7, 3) and (5, 5). No sequence of only 3s works, since 9 and 12 skip over 10.

Input: nums = [9], target = 3
Output: 0

The only available number is larger than the target, so no sequence sums to 3.