Count ordered sequences of share denominations that sum to a target total

Quick Overview

Count the ordered sequences of reusable share denominations that add up to an exact target, returned modulo 10^9 + 7. It tests one-dimensional dynamic programming and how the loop order decides whether different orderings of the same values are counted separately.

Count ordered sequences of share denominations that sum to a target total

Company: Optiver

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an integer array `shares` representing the available share denominations (each value can be used any number of times) and an integer `target` representing the total number of shares to distribute. Return the number of distinct ordered sequences of denominations from `shares` whose values sum to exactly `target`. Two sequences are considered different if the order of denominations differs at any position. Since the answer can be very large, return it modulo `10^9 + 7`. ### Function Signature ```python def count_sequences(shares: list[int], target: int) -> int: ``` ### Rules - Order matters: `[1,2]` and `[2,1]` are two different sequences. - Each value in `shares` can appear in a sequence any number of times, or not at all. - Return the count modulo `10^9 + 7`. If no sequence sums to `target`, return `0`. ### Constraints - `1 <= shares.length <= 200` - `1 <= shares[i] <= 1000` - `1 <= target <= 1000` - All elements of `shares` are distinct. ### Examples ```text Input: shares = [1,2,3], target = 4 Output: 7 ``` The valid ordered sequences summing to 4 are [1,1,1,1], [1,1,2], [1,2,1], [2,1,1], [2,2], [1,3] and [3,1]. That makes 7 sequences. ```text Input: shares = [2,5], target = 7 Output: 2 ``` The valid sequences are [2,5] and [5,2]. Sums of 2s alone are even, and two 5s already exceed 7. ```text Input: shares = [4,6], target = 9 Output: 0 ``` Every sum of 4s and 6s is even, so no sequence reaches 9.

Overview: Count the ordered sequences of reusable share denominations that add up to an exact target, returned modulo 10^9 + 7. It tests one-dimensional dynamic programming and how the loop order decides whether different orderings of the same values are counted separately.

Optiver logo
Optiver
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an integer array shares representing the available share denominations (each value can be used any number of times) and an integer target representing the total number of shares to distribute.

Return the number of distinct ordered sequences of denominations from shares whose values sum to exactly target. Two sequences are considered different if the order of denominations differs at any position.

Since the answer can be very large, return it modulo 10^9 + 7.

Function Signature

def count_sequences(shares: list[int], target: int) -> int:

Rules

  • Order matters: [1,2] and [2,1] are two different sequences.
  • Each value in shares can appear in a sequence any number of times, or not at all.
  • Return the count modulo 10^9 + 7 . If no sequence sums to target , return 0 .

Constraints

  • 1 <= shares.length <= 200
  • 1 <= shares[i] <= 1000
  • 1 <= target <= 1000
  • All elements of shares are distinct.

Examples

Input: shares = [1,2,3], target = 4
Output: 7

The valid ordered sequences summing to 4 are [1,1,1,1], [1,1,2], [1,2,1], [2,1,1], [2,2], [1,3] and [3,1]. That makes 7 sequences.

Input: shares = [2,5], target = 7
Output: 2

The valid sequences are [2,5] and [5,2]. Sums of 2s alone are even, and two 5s already exceed 7.

Input: shares = [4,6], target = 9
Output: 0

Every sum of 4s and 6s is even, so no sequence reaches 9.