🧠
Brute Force (Generate All Permutations and Check)
💡 This approach exists to build foundational understanding by enumerating all permutations and checking the condition. It is straightforward but inefficient, helping beginners grasp the problem's constraints and the need for pruning.
Intuition
Generate every permutation of numbers 1 to n, then check if each permutation satisfies the divisibility condition at every position.
Algorithm
- Generate all permutations of numbers from 1 to n.
- For each permutation, check if for every position i, the number at position i divides i or i divides the number.
- Count the permutations that satisfy the condition.
- Return the count.
💡 This algorithm is conceptually simple but computationally expensive because it does not prune invalid permutations early.
from itertools import permutations
def countArrangement(n: int) -> int:
count = 0
for perm in permutations(range(1, n+1)):
if all((perm[i] % (i+1) == 0) or ((i+1) % perm[i] == 0) for i in range(n)):
count += 1
return count
# Driver code
if __name__ == '__main__':
print(countArrangement(2)) # Output: 2
Line Notes
from itertools import permutationsImport permutations to generate all possible orderings of numbers
for perm in permutations(range(1, n+1))Iterate over every possible permutation of numbers 1 to n
all((perm[i] % (i+1) == 0) or ((i+1) % perm[i] == 0) for i in range(n))Check the divisibility condition for every position in the permutation
count += 1Increment count if the current permutation is a beautiful arrangement
import java.util.*;
public class BeautifulArrangement {
public static int countArrangement(int n) {
List<Integer> nums = new ArrayList<>();
for (int i = 1; i <= n; i++) nums.add(i);
return permute(nums, 0);
}
private static int permute(List<Integer> nums, int start) {
if (start == nums.size()) {
for (int i = 0; i < nums.size(); i++) {
int pos = i + 1;
int val = nums.get(i);
if (val % pos != 0 && pos % val != 0) return 0;
}
return 1;
}
int count = 0;
for (int i = start; i < nums.size(); i++) {
Collections.swap(nums, i, start);
count += permute(nums, start + 1);
Collections.swap(nums, i, start);
}
return count;
}
public static void main(String[] args) {
System.out.println(countArrangement(2)); // Output: 2
}
}
Line Notes
for (int i = 1; i <= n; i++) nums.add(i);Initialize list with numbers 1 to n
if (start == nums.size())Base case: all positions fixed, check validity
if (val % pos != 0 && pos % val != 0) return 0;Check divisibility condition for each position
Collections.swap(nums, i, start);Swap elements to generate permutations
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int count = 0;
bool isBeautiful(const vector<int>& arr) {
for (int i = 0; i < arr.size(); i++) {
int pos = i + 1;
int val = arr[i];
if (val % pos != 0 && pos % val != 0) return false;
}
return true;
}
void permute(vector<int>& arr, int start) {
if (start == arr.size()) {
if (isBeautiful(arr)) count++;
return;
}
for (int i = start; i < arr.size(); i++) {
swap(arr[i], arr[start]);
permute(arr, start + 1);
swap(arr[i], arr[start]);
}
}
int main() {
int n = 2;
vector<int> arr;
for (int i = 1; i <= n; i++) arr.push_back(i);
permute(arr, 0);
cout << count << endl; // Output: 2
return 0;
}
Line Notes
for (int i = 1; i <= n; i++) arr.push_back(i);Initialize vector with numbers 1 to n
if (start == arr.size())Base case: all positions fixed, check if arrangement is beautiful
if (val % pos != 0 && pos % val != 0) return false;Check divisibility condition for each position
swap(arr[i], arr[start]);Swap elements to generate permutations
function countArrangement(n) {
let count = 0;
const nums = [];
for (let i = 1; i <= n; i++) nums.push(i);
function permute(arr, start) {
if (start === arr.length) {
for (let i = 0; i < arr.length; i++) {
let pos = i + 1;
let val = arr[i];
if (val % pos !== 0 && pos % val !== 0) return;
}
count++;
return;
}
for (let i = start; i < arr.length; i++) {
[arr[i], arr[start]] = [arr[start], arr[i]];
permute(arr, start + 1);
[arr[i], arr[start]] = [arr[start], arr[i]];
}
}
permute(nums, 0);
return count;
}
// Test
console.log(countArrangement(2)); // Output: 2
Line Notes
for (let i = 1; i <= n; i++) nums.push(i);Initialize array with numbers 1 to n
if (start === arr.length)Base case: all positions fixed, check validity
if (val % pos !== 0 && pos % val !== 0) return;Check divisibility condition for each position
[arr[i], arr[start]] = [arr[start], arr[i]];Swap elements to generate permutations
count++Increment count if current permutation is beautiful
Generating all permutations takes O(n!) time, and checking each permutation takes O(n) time, resulting in O(n! * n) total time. Space is O(n) for recursion stack and permutation storage.
💡 For n=10, n! is about 3.6 million, so this approach quickly becomes infeasible beyond small n.
Interview Verdict: TLE
This approach times out for larger inputs because it tries all permutations without pruning, demonstrating the need for optimization.