Smallest ascending list of n distinct positive integers that sum to a target

Quick Overview

Build the lexicographically smallest ascending list of n distinct positive integers whose sum equals a given target, or return an empty list when none exists. The problem tests a greedy construction, a lower-bound argument on sums, and careful handling of the feasibility boundary.

Smallest ascending list of n distinct positive integers that sum to a target

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given two integers `n` and `target`, return `n` distinct positive integers whose sum is exactly `target`. Many sets of integers can work. Return the integers in ascending order, and among all valid sets choose the one whose ascending list is lexicographically smallest. If no set of `n` distinct positive integers sums to `target`, return an empty list. ### Function Signature ```python def construct_sequence(n: int, target: int) -> list[int]: ``` ### Rules - The returned list has exactly `n` elements, every element is at least 1, no value repeats, and the elements sum to `target`. - The list is sorted in ascending order. - Lexicographic order compares two lists at the first index where they differ. The list with the smaller value at that index is the smaller list. - Return `[]` when no valid list exists. ### Constraints - `1 <= n <= 10^5` - `1 <= target <= 10^9` ### Examples ```text Input: n = 4, target = 10 Output: [1, 2, 3, 4] ``` The smallest possible sum of 4 distinct positive integers is 1 + 2 + 3 + 4 = 10. That equals the target, so this is the only valid set. ```text Input: n = 3, target = 5 Output: [] ``` Any 3 distinct positive integers sum to at least 1 + 2 + 3 = 6, which is more than 5, so no valid list exists. ```text Input: n = 3, target = 20 Output: [1, 2, 17] ``` 1 + 2 + 17 = 20. An ascending list of distinct positive integers cannot start lower than 1, 2, so this list is smaller than other valid answers such as [1, 3, 16] or [2, 3, 15].

Overview: Build the lexicographically smallest ascending list of n distinct positive integers whose sum equals a given target, or return an empty list when none exists. The problem tests a greedy construction, a lower-bound argument on sums, and careful handling of the feasibility boundary.

Amazon logo
Amazon
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given two integers n and target, return n distinct positive integers whose sum is exactly target.

Many sets of integers can work. Return the integers in ascending order, and among all valid sets choose the one whose ascending list is lexicographically smallest. If no set of n distinct positive integers sums to target, return an empty list.

Function Signature

def construct_sequence(n: int, target: int) -> list[int]:

Rules

  • The returned list has exactly n elements, every element is at least 1, no value repeats, and the elements sum to target .
  • The list is sorted in ascending order.
  • Lexicographic order compares two lists at the first index where they differ. The list with the smaller value at that index is the smaller list.
  • Return [] when no valid list exists.

Constraints

  • 1 <= n <= 10^5
  • 1 <= target <= 10^9

Examples

Input: n = 4, target = 10
Output: [1, 2, 3, 4]

The smallest possible sum of 4 distinct positive integers is 1 + 2 + 3 + 4 = 10. That equals the target, so this is the only valid set.

Input: n = 3, target = 5
Output: []

Any 3 distinct positive integers sum to at least 1 + 2 + 3 = 6, which is more than 5, so no valid list exists.

Input: n = 3, target = 20
Output: [1, 2, 17]

1 + 2 + 17 = 20. An ascending list of distinct positive integers cannot start lower than 1, 2, so this list is smaller than other valid answers such as [1, 3, 16] or [2, 3, 15].