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