968. Beautiful Array
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
Method: Divide and Conquer with Parity
The problem asks us to construct a special permutation of 1 to n called a "beautiful array". The condition for a beautiful array is that for any i < k < j, 2 * nums[k] != nums[i] + nums[j]. This is equivalent to saying that no element nums[k] is the arithmetic mean of nums[i] and nums[j] where nums[k] appears between nums[i] and nums[j] in the array. In other words, there are no three elements forming an arithmetic progression in their positions in the array.
Solution
Beautiful Array in Python
class Solution:
def beautifulArray(self, n: int) -> list[int]:
# Start with the base case for n=1.
res = [1]
# Iteratively build up the beautiful array.
while len(res) < n:
# If A is a beautiful array, then {2*x-1 for x in A} (odds) and
# {2*x for x in A} (evens) are also beautiful for their respective number sets.
# Concatenating them preserves the beautiful property.
odds = [2 * x - 1 for x in res]
evens = [2 * x for x in res]
res = odds + evens
# The generated array might be for a power of 2 larger than n.
# Filter out the numbers that are greater than n.
return [x for x in res if x <= n]
Beautiful Array in Java
class Solution {
public int[] beautifulArray(int n) {
// Start with the base case for n=1.
ArrayList<Integer> res = new ArrayList<>();
res.add(1);
// Iteratively build up the beautiful array.
while (res.size() < n) {
ArrayList<Integer> temp = new ArrayList<>();
// First, generate all odd numbers from the current beautiful array.
// If A is a beautiful array, {2*x - 1 for x in A} is also beautiful.
for (int x : res) {
temp.add(2 * x - 1);
}
// Then, generate all even numbers.
// If A is a beautiful array, {2*x for x in A} is also beautiful.
for (int x : res) {
temp.add(2 * x);
}
// Concatenating these two beautiful sequences (odds and evens)
// results in a new, larger beautiful array.
res = temp;
}
// The generated array might be for a power of 2 larger than n.
// Filter out the numbers that are greater than n and convert to int[].
return res.stream()
.filter(x -> x <= n)
.mapToInt(Integer::intValue)
.toArray();
}
}
Beautiful Array in C++
class Solution {
public:
vector<int> beautifulArray(int n) {
// Start with the base case for n=1.
vector<int> res = {1};
// Iteratively build up the beautiful array.
while (res.size() < n) {
vector<int> temp;
// First, generate all odd numbers from the current beautiful array.
// If A is a beautiful array, {2*x - 1 for x in A} is also beautiful.
for (int x : res) {
temp.push_back(2 * x - 1);
}
// Then, generate all even numbers.
// If A is a beautiful array, {2*x for x in A} is also beautiful.
for (int x : res) {
temp.push_back(2 * x);
}
// Concatenating these two beautiful sequences (odds and evens)
// results in a new, larger beautiful array.
res = temp;
}
// The generated array might be for a power of 2 larger than n.
// Filter out the numbers that are greater than n.
vector<int> final_res;
for (int x : res) {
if (x <= n) {
final_res.push_back(x);
}
}
return final_res;
}
};
Beautiful Array in TypeScript
function beautifulArray(n: number): number[] {
// Start with the base case for n=1.
let res: number[] = [1];
// Iteratively build up the beautiful array.
while (res.length < n) {
// If A is a beautiful array, then {2*x-1 for x in A} (odds) and
// {2*x for x in A} (evens) are also beautiful for their respective number sets.
// Concatenating them preserves the beautiful property.
res = [
...res.map(x => 2 * x - 1),
...res.map(x => 2 * x)
];
}
// The generated array might be for a power of 2 larger than n.
// Filter out the numbers that are greater than n.
return res.filter(x => x <= n);
}
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.