Beautiful Arrangement

MediumBacktrackingbacktracking

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Consider permutations perm of the integers 1..n, using 1-indexed positions. A permutation is **beautiful** if, for every position i, either perm[i] is divisible by i or i is divisible by perm[i]. Given n, return the number of beautiful permutations.

Examples

Input: n = 2
Output: 2
Both [1,2] and [2,1] satisfy the rule at every position.
Input: n = 1
Output: 1
The single permutation [1] is beautiful.

Constraints

  • 1 <= n <= 15

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More backtracking problems