Beautiful Arrangement II
MediumUpdated: Aug 2, 2025
Practice on:
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 = [a1, a2, a3, ... , an], then the list[|a1 - a2|, |a2 - a3|, |a3 - a4|, ... , |an-1 - an|]has exactlykdistinct integers.
Return the list answer. If there multiple valid answers, return any of them.
Examples
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.
Constraints
1 <= k < n <= 10^4
Solution
Method 1 – Constructive Alternating High-Low
Intuition
The key idea is to create exactly k distinct absolute differences by alternating between the smallest and largest available numbers for the first k+1 positions, then filling the rest in order. This guarantees the required number of distinct differences.
Approach
- Initialize two pointers:
l = 1(left) andr = n(right). - For the first
k+1elements, alternate picking fromlandr:- If the current index is even, pick from
land incrementl. - If odd, pick from
rand decrementr.
- If the current index is even, pick from
- After
k+1elements, fill the rest with the remaining numbers in order (either ascending or descending, depending on the last pick). - Return the constructed list.
Code
C++
class Solution {
public:
vector<int> constructArray(int n, int k) {
vector<int> ans;
int l = 1, r = n;
for (int i = 0; i < k + 1; ++i) {
if (i % 2 == 0) ans.push_back(l++);
else ans.push_back(r--);
}
if ((k + 1) % 2 == 0) {
for (int i = r; i >= l; --i) ans.push_back(i);
} else {
for (int i = l; i <= r; ++i) ans.push_back(i);
}
return ans;
}
};
Go
func constructArray(n int, k int) []int {
ans := make([]int, 0, n)
l, r := 1, n
for i := 0; i < k+1; i++ {
if i%2 == 0 {
ans = append(ans, l)
l++
} else {
ans = append(ans, r)
r--
}
}
if (k+1)%2 == 0 {
for i := r; i >= l; i-- {
ans = append(ans, i)
}
} else {
for i := l; i <= r; i++ {
ans = append(ans, i)
}
}
return ans
}
Java
class Solution {
public int[] constructArray(int n, int k) {
int[] ans = new int[n];
int l = 1, r = n, idx = 0;
for (int i = 0; i < k + 1; i++) {
if (i % 2 == 0) ans[idx++] = l++;
else ans[idx++] = r--;
}
if ((k + 1) % 2 == 0) {
for (int i = r; i >= l; i--) ans[idx++] = i;
} else {
for (int i = l; i <= r; i++) ans[idx++] = i;
}
return ans;
}
}
Kotlin
class Solution {
fun constructArray(n: Int, k: Int): IntArray {
val ans = IntArray(n)
var l = 1
var r = n
for (i in 0..k) {
ans[i] = if (i % 2 == 0) l++ else r--
}
if ((k + 1) % 2 == 0) {
for (i in r downTo l) ans[++k] = i
} else {
for (i in l..r) ans[++k] = i
}
return ans
}
}
Python
class Solution:
def constructArray(self, n: int, k: int) -> list[int]:
ans: list[int] = []
l, r = 1, n
for i in range(k + 1):
if i % 2 == 0:
ans.append(l)
l += 1
else:
ans.append(r)
r -= 1
if (k + 1) % 2 == 0:
ans.extend(range(r, l - 1, -1))
else:
ans.extend(range(l, r + 1))
return ans
Rust
impl Solution {
pub fn construct_array(n: i32, k: i32) -> Vec<i32> {
let mut ans = Vec::with_capacity(n as usize);
let (mut l, mut r) = (1, n);
for i in 0..=k {
if i % 2 == 0 {
ans.push(l);
l += 1;
} else {
ans.push(r);
r -= 1;
}
}
if (k + 1) % 2 == 0 {
for i in (l..=r).rev() {
ans.push(i);
}
} else {
for i in l..=r {
ans.push(i);
}
}
ans
}
}
Complexity
- ⏰ Time complexity:
O(n)— Each element is added once. - 🧺 Space complexity:
O(n)— Output array of sizen.