Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

526. Beautiful Arrangement

Medium64.4% Acceptance
ArrayDynamic ProgrammingBacktracking
Asked by:
Cisco
ProblemSolutions (12)VideosCompanies (1)Notes

Problem Statement

Suppose you have n integers labeled 1 through n. A permutation of those n integers perm (1-indexed) is considered a beautiful arrangement if for every i (1 <= i <= n), either of the following is true:

  • perm[i] is divisible by i.
  • i is divisible by perm[i].

Given an integer n, return the number of the beautiful arrangements that you can construct.

Example 1:

Input: n = 2
Output: 2
Explanation: 
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

Constraints:

  • 1 <= n <= 15
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
C

Approach

In #526 Beautiful Arrangement, we need to count permutations of numbers from 1..n such that for every position i, either perm[i] % i == 0 or i % perm[i] == 0. The most effective strategy is backtracking with pruning. We place numbers position by position and only choose values that satisfy the divisibility rule for that index. By tracking used numbers and skipping invalid candidates early, the search space reduces significantly.

An optimized variant uses bitmask dynamic programming. A bitmask represents which numbers have already been used, and the number of set bits determines the current position. The DP state stores how many valid arrangements can be formed from that mask. Precomputing valid numbers for each position further speeds up transitions.

Backtracking works well because n ≤ 15, while the bitmask DP approach provides a structured solution with memoization. The optimized DP solution typically runs in about O(n · 2^n) time with O(2^n) space.

Complexity

ApproachTime ComplexitySpace Complexity
Backtracking with PruningO(n!) worst case, but heavily pruned in practiceO(n)
Bitmask Dynamic ProgrammingO(n × 2^n)O(2^n)

Video Solution Available

Knowledge Center

View all video solutions

Solutions (12)

Backtracking Approach

The idea is to generate permutations of the numbers from 1 to n and check if each permutation is a beautiful arrangement. This can be efficiently done using backtracking. While generating permutations, we can directly test the divisibility conditions to ensure they form a beautiful arrangement.

Time Complexity: O(k), where k are the valid permutations (worst-case roughly n!).
Space Complexity: O(n) due to the recursion stack.

PythonCC++JavaC#JavaScript
1def countArrangement(n):
2    def count(i, x):
3        if i < 2:
4            return 1
5        total 

Explanation

This solution uses bitmasking to mark the numbers that have been used. It recursively checks each position with available numbers. If a number can be placed at a position according to the rules, it tries to place the next number. If a full beautiful arrangement is achieved, it counts it.

Dynamic Programming Approach with Bitmasking

This approach involves using dynamic programming (DP) with bitmasking to efficiently count beautiful arrangements. By representing the set of visited numbers as a bitmask and using memoization, we can avoid redundant calculations.

Time Complexity: O(n * 2^n) due to bitmask permutations.
Space Complexity: O(2^n) for the DP cache plus recursion stack.

PythonCC++JavaC#JavaScript
1def



Video Solutions

Watch expert explanations and walkthroughs

Beautiful Arrangement | LeetCode 526 | C++, Java, Python

Knowledge Center
16:1814,425 views

Asked By Companies

1 companies
C
Cisco

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Trapping Rain WaterHard
Jump Game IIMedium
Maximum SubarrayMedium
Jump GameMedium
More similar problems

Related Topics

ArrayDynamic ProgrammingBacktrackingBit ManipulationBitmask

Problem Stats

Acceptance Rate64.4%
DifficultyMedium
Companies1

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Beautiful Arrangement asked in FAANG interviews?

Problems similar to Beautiful Arrangement appear in technical interviews because they test backtracking, recursion, and bitmask techniques. While the exact problem may not always appear, the pattern of constrained permutation generation is common in FAANG-style interviews.

What is the optimal approach for Beautiful Arrangement?

The optimal approach typically uses bitmask dynamic programming or backtracking with pruning. Bitmask DP tracks which numbers are already used and computes the number of valid permutations efficiently. This reduces redundant work and runs in roughly O(n × 2^n) time.

Why is backtracking effective for Beautiful Arrangement?

Backtracking works well because the constraints allow strong pruning. At each position, you only try numbers that satisfy the divisibility condition, which eliminates many permutations early. Since n is at most 15, the reduced search space makes the approach practical.

What data structure is best for solving Beautiful Arrangement?

A bitmask is commonly used to represent which numbers are already included in the permutation. Combined with a DP array or memoization map, it allows efficient state transitions and avoids recomputation of subproblems.

=
0
6
for
j
in
range
(
n
)
:
7
if
not
(
x
&
(
1
<<
j
)
)
and
(
(
i
%
(
j
+
1
)
==
0
)
or
(
(
j
+
1
)
%
i
==
0
)
)
:
8
total
+=
count
(
i
-
1
,
x
|
(
1
<<
j
)
)
9
return
total
10
return
count
(
n
,
0
)
countArrangement
(
n
)
:
2
dp
=
[
-
1
]
*
(
1
<<
n
)
3
4
def
dfs
(
mask
,
i
)
:
5
if
i
==
0
:
6
return
1
7
if
dp
[
mask
]
!=
-
1
:
8
return
dp
[
mask
]
9
10
total
=
0
11
for
j
in
range
(
n
)
:
12
if
not
mask
&
(
1
<<
j
)
and
(
(
i
%
(
j
+
1
)
==
0
)
or
(
(
j
+
1
)
%
i
==
0
)
)
:
13
total
+=
dfs
(
mask
|
(
1
<<
j
)
,
i
-
1
)
14
15
dp
[
mask
]
=
total
16
return
total
17
18
return
dfs
(
0
,
n
)

Explanation

This Python solution uses a top-down dynamic programming approach with memoization. The function dfs uses a bitmask to denote which integers have been placed, and iteratively places integers satisfying the beautiful arrangement condition.