LeetCode Problem

How to Solve Beautiful Arrangement

The Beautiful Arrangement problem requires finding permutations of n integers where each integer must meet divisibility conditions. Using dynamic programming and backtracking, the solution explores all valid arrangements efficiently. The challenge lies in implementing a state transition model that handles the constraints effectively.

GhostInterview Help

Need help with Beautiful Arrangement without spending extra time grinding it?

GhostInterview can read Beautiful Arrangement from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #526State transition dynamic programmingReviewed 2026-03-07
Difficulty
Medium
Primary pattern
State transition dynamic programming
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

The Beautiful Arrangement problem requires finding permutations of n integers where each integer must meet divisibility conditions. Using dynamic programming and backtracking, the solution explores all valid arrangements efficiently. The challenge lies in implementing a state transition model that handles the constraints effectively.

Problem Statement

Given an integer n, you are tasked with constructing permutations of integers 1 through n. A permutation is considered a beautiful arrangement if for each position i (1 ≤ i ≤ n), one of the following is true: either the integer at position i is divisible by i, or i is divisible by the integer at position i.

Your goal is to return the number of valid beautiful arrangements that can be constructed for the given n. The problem can be tackled using dynamic programming and backtracking techniques to efficiently generate and count valid arrangements.

Examples

Example 1

Input: n = 2

Output: 2

The first beautiful arrangement is [1,2]:

  • perm[1] = 1 is divisible by i = 1
  • perm[2] = 2 is divisible by i = 2 The second beautiful arrangement is [2,1]:
  • perm[1] = 2 is divisible by i = 1
  • i = 2 is divisible by perm[2] = 1

Example 2

Input: n = 1

Output: 1

Example details omitted.

Constraints

  • 1 <= n <= 15

Solution Approach

Backtracking with State Transitions

The problem can be approached by recursively attempting to build the arrangement, checking the divisibility condition at each step. At each position, backtracking allows the exploration of different valid configurations, while dynamic programming is employed to remember previous state results to avoid redundant calculations.

Bitmask Dynamic Programming

This approach utilizes a bitmask to represent which numbers have been used in the arrangement. By iterating over the unused numbers and checking the divisibility constraints, we can efficiently count the valid arrangements through dynamic state transitions.

Pruning with Constraints

Incorporating constraints such as the divisibility checks in each step helps prune invalid paths early, minimizing unnecessary exploration. This makes the backtracking approach more efficient by reducing the search space and improving performance.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity depends on the final approach but generally involves recursion with memoization or bitmasking, leading to a complexity of O(n!) in the worst case. Space complexity is influenced by the storage of state information and intermediate results, such as the bitmask or dynamic programming tables, making it O(n) or O(n!) depending on the implementation details.

What Interviewers Usually Probe

  • Understanding of dynamic programming principles and backtracking.
  • Ability to optimize search space using bitmasking.
  • Familiarity with state transitions and pruning techniques.

Common Pitfalls or Variants

Common pitfalls

  • Overlooking the need for pruning, leading to excessive recursion.
  • Failing to properly handle base cases or conditions during recursion.
  • Mismanaging state transitions when using dynamic programming or bitmasking.

Follow-up variants

  • Increasing n beyond 15 to test scalability.
  • Modifying the divisibility rule for more complex conditions.
  • Limiting the problem to only odd or even positions.

How GhostInterview Helps

  • GhostInterview offers step-by-step guidance on breaking down the problem using dynamic programming and backtracking.
  • The platform helps you implement a bitmask solution, optimizing state transitions to handle large inputs.
  • GhostInterview prepares you for interviews by refining your approach to efficiently handle complex constraints.

Topic Pages

FAQ

What is the pattern behind the Beautiful Arrangement problem?

The problem primarily revolves around state transition dynamic programming, utilizing backtracking and bitmasking techniques to efficiently find valid permutations.

How can backtracking help with the Beautiful Arrangement problem?

Backtracking allows you to explore possible valid arrangements by trying each number in each position while checking the divisibility conditions. It helps prune invalid paths early, saving time.

What is the time complexity of solving the Beautiful Arrangement problem?

The time complexity generally involves recursion with dynamic programming or bitmasking, leading to O(n!) in the worst case, depending on the implementation approach.

How can I optimize my solution for larger n values?

Optimizing with memoization, dynamic programming, and bitmasking significantly reduces the complexity, enabling the solution to scale for larger n values.

What is a common mistake when implementing the Beautiful Arrangement solution?

A common mistake is failing to manage the divisibility condition properly, leading to invalid permutations or incorrect counts.

GhostInterview Solver

Need direct help with Beautiful Arrangement instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Beautiful Arrangement from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.