526. Beautiful Arrangement LeetCode Solution

In this guide, you will get 526. Beautiful Arrangement LeetCode Solution with the best time and space complexity. The solution to Beautiful Arrangement problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Beautiful Arrangement solution in C++
  4. Beautiful Arrangement solution in Java
  5. Beautiful Arrangement solution in Python
  6. Additional Resources
526. Beautiful Arrangement LeetCode Solution image

Problem Statement of Beautiful Arrangement

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

Complexity Analysis

  • Time Complexity: O(n \cdot 2^n)
  • Space Complexity: O(2^n)

526. Beautiful Arrangement LeetCode Solution in C++

class Solution {
 public:
  int countArrangement(int n) {
    return dfs(n, 1, string(n + 1, 'x'), {});
  }

 private:
  int dfs(int n, int num, string&& filled, unordered_map<string, int>&& mem) {
    if (num == n + 1)
      return 1;
    if (const auto it = mem.find(filled); it != mem.cend())
      return it->second;

    int count = 0;

    for (int i = 1; i <= n; ++i)
      if (filled[i] == 'x' && (num % i == 0 || i % num == 0)) {
        filled[i] = 'o';
        count += dfs(n, num + 1, std::move(filled), std::move(mem));
        filled[i] = 'x';
      }

    return mem[filled] = count;
  }
};
/* code provided by PROGIEZ */

526. Beautiful Arrangement LeetCode Solution in Java

class Solution {
  public int countArrangement(int n) {
    final String filled = "x".repeat(n + 1);
    StringBuilder sb = new StringBuilder(filled);
    Map<String, Integer> mem = new HashMap<>();

    return dfs(n, 1, sb, mem);
  }

  private int dfs(int n, int num, StringBuilder sb, Map<String, Integer> mem) {
    if (num == n + 1)
      return 1;
    final String filled = sb.toString();
    if (mem.containsKey(filled))
      return mem.get(filled);

    int count = 0;

    for (int i = 1; i <= n; ++i)
      if (sb.charAt(i) == 'x' && (num % i == 0 || i % num == 0)) {
        sb.setCharAt(i, 'o');
        count += dfs(n, num + 1, sb, mem);
        sb.setCharAt(i, 'x');
      }

    mem.put(filled, count);
    return count;
  }
}
// code provided by PROGIEZ

526. Beautiful Arrangement LeetCode Solution in Python

N/A
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.