An array nums
of length n
is beautiful if:
nums
is a permutation of the integers in the range [1, n]
.0 <= i < j < n
, there is no index k
with i < k < j
where 2 * nums[k] == nums[i] + nums[j]
.Given the integer n
, return any beautiful array nums
of length n
. There will be at least one valid answer for the given n
.
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
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to creating a 'beautiful array' involves testing every possible arrangement of numbers. We generate each possible array, then we check if that array meets the 'beautiful' criteria. This guarantees we will find a 'beautiful' array if one exists.
Here's how the algorithm would work step-by-step:
import itertools
def is_beautiful(arrangement):
for i in range(len(arrangement)):
for j in range(i + 1, len(arrangement)):
sum_of_elements = arrangement[i] + arrangement[j]
if sum_of_elements % 2 == 0:
average = sum_of_elements // 2
if average in arrangement[i+1:j]:
return False
return True
def beautiful_array_brute_force(number):
numbers = list(range(1, number + 1))
possible_arrangements = itertools.permutations(numbers)
for arrangement in possible_arrangements:
# Check all permutations to meet criteria
if is_beautiful(arrangement):
return list(arrangement)
# If there are no beautiful arrangements, return empty.
return []
The key idea is to divide the problem into smaller, self-similar subproblems. We build the beautiful array from smaller beautiful arrays. Odd and even numbers can be placed in separate halves which guarantees the property.
Here's how the algorithm would work step-by-step:
def beautiful_array(number):
beautiful_array_result = [1]
# Build beautiful arrays iteratively.
while len(beautiful_array_result) < number:
temp_array = []
# Generate odd numbers.
for element in beautiful_array_result:
temp_array.append(2 * element - 1)
# Generate even numbers.
for element in beautiful_array_result:
temp_array.append(2 * element)
beautiful_array_result = temp_array
# Filter out numbers exceeding the range.
beautiful_array_result = [element for element in beautiful_array_result if element <= number]
# Ensures the result array has the correct size
return beautiful_array_result
Case | How to Handle |
---|---|
Input n is 1 | Return [1] since a single element array is trivially beautiful. |
Input n is 2 | Return [1,2] as the only beautiful array of size 2. |
Large input n leading to deep recursion | Use dynamic programming/memoization within the recursive calls to avoid stack overflow and improve performance. |
Negative input for n | Raise an IllegalArgumentException/ValueError as n must be a positive integer. |
n is a power of 2 | Observe the pattern that beautiful arrays for powers of 2 can be efficiently constructed recursively. |
Integer overflow during calculations (e.g., mid * 2) | Ensure intermediate calculations avoid potential integer overflows by using appropriate data types (e.g., long). |
Non-integer input for n | Raise an IllegalArgumentException/ValueError as n must be an integer. |
Extreme value of n (near Integer.MAX_VALUE) | Ensure memory allocation for the array does not exceed available resources or lead to out-of-memory errors. |