Problem · Dynamic Programming
Beautiful Arrangement
Learn this problemProblem statement
Given an integer n, consider permutations of the integers from 1 through n.
A permutation perm is a beautiful arrangement when, for every 1-indexed position i, at least one of these conditions holds:
perm[i]is divisible byi.iis divisible byperm[i].
Return the number of beautiful arrangements.
Function
countArrangement(n: int) → intExamples
Example 1
n = 2return = 2The valid arrangements are [1,2] and [2,1]. Each value is compatible with its 1-indexed position.
Example 2
n = 1return = 1The only arrangement is [1], and 1 is compatible with position 1.
Constraints
1 <= n <= 15
More MathWorks problems
- Find Minimum Cost to Remove Array ElementsONSITE INTERVIEW · Seen Jul 2026
- Group Shifted StringsONSITE INTERVIEW · Seen Jul 2026
- Longest Valid ParenthesesONSITE INTERVIEW · Seen Jul 2026
- Balancing TeamsOA · Seen Nov 2025
- Largest Sub-GridOA · Seen Nov 2025
- Discount TagsOA · Seen Oct 2024
- Odd One OutOA · Seen Oct 2024
- Alloy ProductionOA · Seen Sep 2024