Beautiful Arrangement

Suppose you have `n` integers labeled `1` through `n`. A permutation `perm` (1-indexed) of those integers is a beautiful arrangement 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 arrangements you can construct.

30:00

Beautiful Arrangement
medium
Free written solution
Topics
Companies

Suppose you have n integers labeled 1 through n. A permutation perm (1-indexed) of those integers is a beautiful arrangement 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 arrangements you can construct.

Example 1:
Input: {"n":2}
Output: 2
Constraints:
  • 1≤n≤151 \leq n \leq 15

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":2}

pos\num

1

2

2

1

blue dot: number fits that position (one divides the other) · struck column: number already used

·

1

·

2

Fill positions from 2 down to 1. Large positions have the fewest numbers that fit, so dead ends show up early.

arrangements found: 0
Variables
VariableValue
pos2
count0
DepthFunction Call
Stack empty
0/11