Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 <= 1000Problem summary: An array nums of length n is beautiful if: nums is a permutation of the integers in the range [1, n]. For every 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
4
5
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #932: Beautiful Array
class Solution {
public int[] beautifulArray(int n) {
if (n == 1) {
return new int[] {1};
}
int[] left = beautifulArray((n + 1) >> 1);
int[] right = beautifulArray(n >> 1);
int[] ans = new int[n];
int i = 0;
for (int x : left) {
ans[i++] = x * 2 - 1;
}
for (int x : right) {
ans[i++] = x * 2;
}
return ans;
}
}
// Accepted solution for LeetCode #932: Beautiful Array
func beautifulArray(n int) []int {
if n == 1 {
return []int{1}
}
left := beautifulArray((n + 1) >> 1)
right := beautifulArray(n >> 1)
var ans []int
for _, x := range left {
ans = append(ans, x*2-1)
}
for _, x := range right {
ans = append(ans, x*2)
}
return ans
}
# Accepted solution for LeetCode #932: Beautiful Array
class Solution:
def beautifulArray(self, n: int) -> List[int]:
if n == 1:
return [1]
left = self.beautifulArray((n + 1) >> 1)
right = self.beautifulArray(n >> 1)
left = [x * 2 - 1 for x in left]
right = [x * 2 for x in right]
return left + right
// Accepted solution for LeetCode #932: Beautiful Array
struct Solution;
impl Solution {
fn beautiful_array(n: i32) -> Vec<i32> {
if n == 1 {
vec![1]
} else {
let left = Self::beautiful_array(n / 2);
let right = Self::beautiful_array((n + 1) / 2);
left.into_iter()
.map(|x| x * 2)
.chain(right.into_iter().map(|x| x * 2 - 1))
.collect()
}
}
}
#[test]
fn test() {
let n = 4;
let res = vec![4, 2, 3, 1];
assert_eq!(Solution::beautiful_array(n), res);
}
// Accepted solution for LeetCode #932: Beautiful Array
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #932: Beautiful Array
// class Solution {
// public int[] beautifulArray(int n) {
// if (n == 1) {
// return new int[] {1};
// }
// int[] left = beautifulArray((n + 1) >> 1);
// int[] right = beautifulArray(n >> 1);
// int[] ans = new int[n];
// int i = 0;
// for (int x : left) {
// ans[i++] = x * 2 - 1;
// }
// for (int x : right) {
// ans[i++] = x * 2;
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.