Skip to content

Beautiful Arrangement

MediumBacktracking
Decode it first. Clues, constraints, pattern and plan before any code.

Arrange the numbers 1 to n in a row, using each exactly once. Positions are numbered from 1. The arrangement is beautiful if, at every position i, the number placed there divides i or i divides the number. Return how many beautiful arrangements exist.

n = 2  →  2   ([1, 2] and [2, 1] both work)
n = 3  →  3   ([1, 2, 3], [2, 1, 3], [3, 2, 1])
n = 1  →  1

Constraints

  • 1 <= n <= 15