class Solution:
def constructArray(self, n: int, k: int) -> list[int]:
answer = []
left, right = 1, n
for i in range(k):
if i % 2 == 0:
answer.append(left)
left += 1
else:
answer.append(right)
right -= 1
# Fill the rest in order
if k % 2 == 0:
answer.extend(range(right, left - 1, -1))
else:
answer.extend(range(left, right + 1))
return answer
class Solution {
public:
vector<int> constructArray(int n, int k) {
vector<int> answer;
int left = 1, right = n;
for (int i = 0; i < k; ++i) {
if (i % 2 == 0) {
answer.push_back(left++);
} else {
answer.push_back(right--);
}
}
if (k % 2 == 0) {
for (int i = right; i >= left; --i)
answer.push_back(i);
} else {
for (int i = left; i <= right; ++i)
answer.push_back(i);
}
return answer;
}
};
class Solution {
public int[] constructArray(int n, int k) {
int[] answer = new int[n];
int left = 1, right = n;
int idx = 0;
for (int i = 0; i < k; ++i) {
if (i % 2 == 0) {
answer[idx++] = left++;
} else {
answer[idx++] = right--;
}
}
if (k % 2 == 0) {
for (int i = right; i >= left; --i) {
answer[idx++] = i;
}
} else {
for (int i = left; i <= right; ++i) {
answer[idx++] = i;
}
}
return answer;
}
}
var constructArray = function(n, k) {
let answer = [];
let left = 1, right = n;
for (let i = 0; i < k; ++i) {
if (i % 2 === 0) {
answer.push(left++);
} else {
answer.push(right--);
}
}
if (k % 2 === 0) {
for (let i = right; i >= left; --i) {
answer.push(i);
}
} else {
for (let i = left; i <= right; ++i) {
answer.push(i);
}
}
return answer;
};
Given two integers n
and k
, construct an array answer
of length n
such that:
1
to n
exactly once (no repeats).k
distinct values.
At first glance, the problem seems to ask for a specific arrangement of numbers with a complex difference pattern. A brute-force approach would be to try all permutations, but this is computationally infeasible for large n
.
The key insight is to recognize that we can control the number of distinct differences by carefully alternating between the smallest and largest available numbers. This creates large and small jumps, maximizing the number of distinct differences early, and then we can fill in the rest in order to avoid introducing new differences.
Here's how we can efficiently construct such an array:
left = 1
and right = n
.
k
positions, alternate picking from the left
and right
pointers:
left
and increment it.right
and decrement it.k
distinct differences.
k
steps, fill the remaining positions with the numbers left, in order (either ascending or descending, depending on the parity of k
), to avoid introducing new differences.
k
distinct differences.
This approach is efficient and guarantees a valid solution.
Let's walk through the process with n = 7
and k = 3
:
left = 1
, right = 7
, answer = []
i = 0
(even): pick 1
(left
), increment left
to 2. i = 1
(odd): pick 7
(right
), decrement right
to 6. i = 2
(even): pick 2
(left
), increment left
to 3. k = 3
steps are done. Remaining numbers: 3, 4, 5, 6.k
is odd, fill in order from left
to right
: 3, 4, 5, 6.
n
numbers, which is O(n!) time and space. This is not feasible for large n
.
n
elements once, so the time complexity is O(n). We use an array of size n
for the answer, so space complexity is also O(n).
The Beautiful Arrangement II problem can be solved elegantly by alternating between picking the smallest and largest available numbers for the first k
steps, then filling in the rest in order. This guarantees exactly k
distinct adjacent differences, meets the constraints, and is highly efficient. The key insight is realizing how alternating selections maximize difference diversity, and that the rest can be filled without introducing new differences.