589. Number of Self-Divisible Permutations

Given an integer n. The task is to determine the number of valid permutations of the sequence arr = [1, 2, ..., n] that satisfy the self-divisibility condition.

A self-divisible permutation is an arrangement where, for every 1 <= i <= n: gcd(arr[i],i)=1, where arr[i] represents the number at index i (1-based indexing).

Example 1:

Input: n = 1

Output:1

Explanation: The only possible sequence [1] is self-divisible.

Example 2:

Input: n = 3

Output: 3

Explanation:

The permutations of [1,2,3] that satisfy the condition are:

[1,3,2]

[3,1,2]

[2,3,1]

Other permutations fail to satisfy the condition for at least one index.

Now Your Turn!

Pick the correct output for the given input

Input: n = 4

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n <= 12

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int selfDivisiblePermutationCount(int n) {
}
 
};
 
Test Case

Input:

N