Beautiful Arrangement II - Problem
Beautiful Arrangement II is a fascinating array construction problem that challenges you to create a specific pattern of differences between adjacent elements.

๐ŸŽฏ The Challenge: Given two integers n and k, you need to construct an array of n different positive integers (ranging from 1 to n) such that the absolute differences between adjacent elements produce exactly k distinct values.

๐Ÿ“Š What you're doing: If your array is [aโ‚, aโ‚‚, aโ‚ƒ, ..., aโ‚™], then the difference array [|aโ‚ - aโ‚‚|, |aโ‚‚ - aโ‚ƒ|, |aโ‚ƒ - aโ‚„|, ..., |aโ‚™โ‚‹โ‚ - aโ‚™|] must contain exactly k unique values.

Example: For n=4, k=2, one valid answer is [1,2,4,3] because the differences are [1,2,1] which has exactly 2 distinct values: {1, 2}.

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 4, k = 2
โ€บ Output: [1, 4, 2, 3]
๐Ÿ’ก Note: The differences are |1-4|=3, |4-2|=2, |2-3|=1. We have 3 differences but only 2 distinct values {2, 3} if we consider the optimal construction pattern. Actually, this gives us {1, 2, 3} which has 3 distinct values. A better answer is [1, 2, 4, 3] giving differences [1, 2, 1] with exactly 2 distinct values: {1, 2}.
example_2.py โ€” Minimum Case
$ Input: n = 3, k = 1
โ€บ Output: [1, 2, 3]
๐Ÿ’ก Note: When k=1, we need exactly 1 distinct difference. The array [1, 2, 3] gives differences [1, 1], which has exactly 1 distinct value: {1}.
example_3.py โ€” Maximum Differences
$ Input: n = 5, k = 4
โ€บ Output: [1, 5, 2, 4, 3]
๐Ÿ’ก Note: Using the alternating pattern: differences are |1-5|=4, |5-2|=3, |2-4|=2, |4-3|=1. This gives us exactly 4 distinct values: {1, 2, 3, 4}.

Visualization

Tap to expand
๐ŸŽต Beautiful Arrangement II: Musical Pattern๐ŸŽผ Piano Keys (n=6, k=3)123456๐ŸŽน Alternating Pattern (k+1=4 notes)1625|1-6| = 5|6-2| = 4|2-5| = 3๐Ÿ“Š Interval Counting543โœ“ Exactly k=3 distinct intervals!๐ŸŽต Complete MelodyResult: [1, 6, 2, 5] โ†’ Fill remaining positions consecutivelyFinal array: [1, 6, 2, 5, 3, 4]๐ŸŽผ Beautiful arrangement with exactly 3 distinct intervals!โšก Key InsightAlternating between extremes creates maximum interval diversity efficientlyTime: O(n) | Space: O(1) - No complex searching needed!
Understanding the Visualization
1
๐ŸŽผ Set the Range
Like a piano with keys 1 to n, position your hands at both ends
2
๐ŸŽน Alternate Extremes
Play alternating high-low pattern: creates maximum interval variety efficiently
3
๐Ÿ“Š Count Intervals
Each alternation creates a new interval size: n-1, n-2, n-3, etc.
4
๐ŸŽต Fill with Scale
Once you have k intervals, complete the melody with a simple scale
Key Takeaway
๐ŸŽฏ Key Insight: The alternating extremes pattern is like creating dramatic musical intervals - by jumping between the highest and lowest available notes, we efficiently generate exactly the number of distinct intervals we need, then complete the melody smoothly.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n! ร— n)

n! permutations, each taking O(n) time to check differences

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Space for current permutation and set to track differences

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค k < n โ‰ค 104
  • The array must contain all integers from 1 to n exactly once
  • k must be less than n (since maximum possible distinct differences is n-1)
Asked in
Google 28 Microsoft 15 Amazon 12 Meta 8
38.4K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen