Beautiful Array
MediumUpdated: Aug 2, 2025
Practice on:
Problem
An array nums of length n is beautiful if:
numsis a permutation of the integers in the range[1, n].- For every
0 <= i < j < n, there is no indexkwithi < k < jwhere2 * nums[k] == nums[i] + nums[j].
Given the integer n, return _anybeautiful array _nums of lengthn.
There will be at least one valid answer for the given n.
Examples
Example 1
Input: n = 4
Output: [2,1,4,3]
Example 2
Input: n = 5
Output: [3,1,2,5,4]
Constraints
1 <= n <= 1000
Solution
Method 1 – Divide and Conquer
Intuition
The key idea is to recursively build a beautiful array by separating numbers into odds and evens. By ensuring that all odd numbers are grouped together and all even numbers are grouped together, we avoid creating any triplet (i, k, j) where 2 * nums[k] == nums[i] + nums[j]. This works because the sum of two odd or two even numbers is always even, but the sum of an odd and an even is odd, so their average is never an integer in the other group.
Approach
- If n == 1, return [1].
- Recursively construct a beautiful array for the odd indices (1, 3, 5, ...) and for the even indices (2, 4, 6, ...).
- For the odd part, map each element x to 2*x - 1.
- For the even part, map each element x to 2*x.
- Concatenate the odd and even results to form the final array.
Code
C++
class Solution {
public:
vector<int> beautifulArray(int n) {
if (n == 1) return {1};
vector<int> ans, odd = beautifulArray((n + 1) / 2), even = beautifulArray(n / 2);
for (int x : odd) ans.push_back(2 * x - 1);
for (int x : even) ans.push_back(2 * x);
return ans;
}
};
Go
func beautifulArray(n int) []int {
if n == 1 {
return []int{1}
}
odd := beautifulArray((n + 1) / 2)
even := beautifulArray(n / 2)
ans := make([]int, 0, n)
for _, x := range odd {
ans = append(ans, 2*x-1)
}
for _, x := range even {
ans = append(ans, 2*x)
}
return ans
}
Java
class Solution {
public int[] beautifulArray(int n) {
if (n == 1) return new int[]{1};
int[] odd = beautifulArray((n + 1) / 2);
int[] even = beautifulArray(n / 2);
int[] ans = new int[n];
int idx = 0;
for (int x : odd) ans[idx++] = 2 * x - 1;
for (int x : even) ans[idx++] = 2 * x;
return ans;
}
}
Kotlin
class Solution {
fun beautifulArray(n: Int): IntArray {
if (n == 1) return intArrayOf(1)
val odd = beautifulArray((n + 1) / 2)
val even = beautifulArray(n / 2)
val ans = IntArray(n)
var idx = 0
for (x in odd) ans[idx++] = 2 * x - 1
for (x in even) ans[idx++] = 2 * x
return ans
}
}
Python
class Solution:
def beautifulArray(self, n: int) -> list[int]:
if n == 1:
return [1]
odd = self.beautifulArray((n + 1) // 2)
even = self.beautifulArray(n // 2)
ans: list[int] = [2 * x - 1 for x in odd] + [2 * x for x in even]
return ans
Rust
impl Solution {
pub fn beautiful_array(n: i32) -> Vec<i32> {
if n == 1 {
return vec![1];
}
let odd = Self::beautiful_array((n + 1) / 2);
let even = Self::beautiful_array(n / 2);
let mut ans = Vec::with_capacity(n as usize);
for &x in &odd {
ans.push(2 * x - 1);
}
for &x in &even {
ans.push(2 * x);
}
ans
}
}
Complexity
- ⏰ Time complexity: O(n)
- 🧺 Space complexity: O(n)