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:
3
💡 Note:
Valid arrangements are [1,2,3], [2,1,3], and [3,2,1]. For [1,2,3]: 1%1=0, 2%2=0, 3%3=0. For [2,1,3]: 2%1=0, 1%2≠0 but 2%1=0, 3%3=0. For [3,2,1]: 3%1=0, 2%2=0, 1%3≠0 but 3%1=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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code