Problem · Dynamic Programming

Beautiful Arrangement

Learn this problem
MediumMathWorks logoMathWorksFULLTIMEONSITE INTERVIEW

Problem 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 by i.
  • i is divisible by perm[i].

Return the number of beautiful arrangements.

Function

countArrangement(n: int) → int

Examples

Example 1

n = 2return = 2

The valid arrangements are [1,2] and [2,1]. Each value is compatible with its 1-indexed position.

Example 2

n = 1return = 1

The only arrangement is [1], and 1 is compatible with position 1.

Constraints

  • 1 <= n <= 15

More MathWorks problems

drafts saved locally
public int countArrangement(int n) {
  // Write your code here.
}
n2
expected2
checking account