#667
Beautiful Arrangement II
specialist · 765 · lc medium +31 · verified · 61.1% accepted · 826 likes · top 61%
Description
Given integers n and k, build an array answer containing each integer from 1 to n exactly once such that the sequence of absolute differences between consecutive elements has exactly k distinct values. Return any valid arrangement.
Example 1:
Input: n = 3, k = 1
Output: [1,2,3]
Explanation: The [1,2,3] has three different positive integers ranging from 1 to 3, and the [1,1] has exactly 1 distinct integer: 1
Example 2:
Input: n = 3, k = 2
Output: [1,3,2]
Explanation: The [1,3,2] has three different positive integers ranging from 1 to 3, and the [2,1] has exactly 2 distinct integers: 1 and 2.
Code
1
2
3