Bird
Raised Fist0
Interview PrepbacktrackingmediumAmazonGoogle

Beautiful Arrangement

Choose your preparation mode3 modes available
📋
Problem

Imagine you are arranging numbered tiles in a row, but only certain positions are allowed for each tile based on divisibility rules. How many beautiful arrangements can you create?

Given an integer n, count the number of the beautiful arrangements that you can construct. A beautiful arrangement is defined as an array of the numbers from 1 to n such that for every position i (1-indexed), either the number at position i is divisible by i or i is divisible by the number.

1 ≤ n ≤ 15
Edge cases: n = 1 → output: 1 (only one number, trivially beautiful)n = 0 → output: 0 (no numbers, no arrangements)n = 15 → output: large number, tests efficiency
</>
IDE
def countArrangement(n: int) -> int:public int countArrangement(int n)int countArrangement(int n)function countArrangement(n)
def countArrangement(n: int) -> int:
    # Write your solution here
    pass
class Solution {
    public int countArrangement(int n) {
        // Write your solution here
        return 0;
    }
}
#include <vector>
using namespace std;

int countArrangement(int n) {
    // Write your solution here
    return 0;
}
function countArrangement(n) {
    // Write your solution here
}
0/9
Common Bugs to Avoid
Wrong: 0Returning 0 for all inputs due to missing base case or incorrect initialization.Add explicit base case: if n == 0: return 0; initialize count properly.
Wrong: 1Always returning 1 regardless of input, ignoring permutations and divisibility checks.Implement full backtracking with divisibility checks instead of constant return.
Wrong: Incorrect count less than expectedGreedy approach that picks numbers without exploring all valid permutations.Use backtracking with pruning instead of greedy selection.
Wrong: Count greater than expectedReusing numbers multiple times, ignoring 0/1 usage constraint.Track used numbers with boolean array or bitmask to avoid reuse.
Wrong: Wrong count due to off-by-one errorsUsing zero-based indexing inconsistently for positions and numbers in divisibility checks.Use 1-based indexing consistently for positions and numbers.
Test Cases
t1_01basic
Input{"n":2}
Expected2

The two beautiful arrangements are [1,2] and [2,1]. Both satisfy the divisibility conditions at each position.

t1_02basic
Input{"n":3}
Expected3

The three beautiful arrangements for n=3 are [1,2,3], [3,2,1], and [2,1,3].

t2_01edge
Input{"n":0}
Expected1

No numbers means no arrangements, so the count is 0.

t2_02edge
Input{"n":1}
Expected1

Only one number and one position, trivially satisfying the divisibility condition.

t2_03edge
Input{"n":15}
Expected24679

Maximum input size to test efficiency and correctness; known result is 24679.

t3_01corner
Input{"n":4}
Expected8

For n=4, the count is 8. This test catches greedy approach failures.

t3_02corner
Input{"n":5}
Expected10

For n=5, the count is 10. This test catches confusion between 0/1 and unbounded usage of numbers.

t3_03corner
Input{"n":6}
Expected36

For n=6, the count is 36. This test catches off-by-one errors in indexing positions or numbers.

t4_01performance
Input{"n":15}
⏱ Performance - must finish in 2000ms

Input n=15 tests performance of backtracking with pruning. Algorithm must complete within 2 seconds.