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].