Beautiful Arrangement - Problem
Suppose you have n integers labeled 1 through n. A permutation of those n integers perm (1-indexed) is considered a beautiful arrangement if for every i (1 <= i <= n), either of the following is true:
perm[i]is divisible byiiis divisible byperm[i]
Given an integer n, return the number of the beautiful arrangements that you can construct.
Input & Output
Example 1 — Small Case
$
Input:
n = 2
›
Output:
2
💡 Note:
Both [1,2] and [2,1] are beautiful: 1%1=0, 2%2=0 for first; 2%1=0, 1%2≠0 but 2%1=0 for second
Example 2 — Moderate Case
$
Input:
n = 3
›
Output:
2
💡 Note:
Valid arrangements are [2,1,3] and [3,2,1]. For [2,1,3]: 2%1=0, 1%2≠0 but 2%1=0, 3%3=0. All conditions satisfied.
Example 3 — Single Element
$
Input:
n = 1
›
Output:
1
💡 Note:
Only arrangement [1] where 1%1=0, so it's beautiful
Constraints
- 1 ≤ n ≤ 15
Visualization
Tap to expand
Understanding the Visualization
1
Input
Integer n representing range [1,2,...,n]
2
Check Rule
For each position i, either perm[i] % i == 0 OR i % perm[i] == 0
3
Count Valid
Return total number of beautiful arrangements
Key Takeaway
🎯 Key Insight: Backtracking with early pruning eliminates invalid arrangements without full exploration
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code