Skip to main content
LeetCode 667, Medium. Topics: Array, Math. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 19 parametrized pytest cases, and a playground notebook:

Problem

Given two integers n and k, construct a list answer that contains n different positive integers ranging from 1 to n and obeys the following requirement:
  • Suppose this list is answer = [a<sub>1</sub>, a<sub>2</sub>, a<sub>3</sub>, ..., a<sub>n</sub>], then the list [|a<sub>1</sub> - a<sub>2</sub>|, |a<sub>2</sub> - a<sub>3</sub>|, |a<sub>3</sub> - a<sub>4</sub>|, ..., |a<sub>n-1</sub> - a<sub>n</sub>|] has exactly k distinct integers.
Return the list answer. If there are multiple valid answers, return any of them.

Examples

Constraints

  • 1 <= k < n <= 10^4

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026