Count permutations with exactly n inversions
Count Permutations with Exactly N Inversions
You are given two integers m and n.
Consider all permutations of the array [1, 2, ..., m] (where each number from 1 to m appears exactly once). A permutation perm (0-indexed) has an inversion pair (x, y) if:
0 <= x < y < mperm[x] > perm[y]
A permutation is called special if it contains exactly n inversions.
Return the number of special permutations modulo 10^9 + 7.
Examples
- Input:
m = 3, n = 0Output:1Explanation: Only[1, 2, 3]has 0 inversions. - Input:
m = 3, n = 1Output:2Explanation:[1, 3, 2]and[2, 1, 3]each have exactly 1 inversion.
Constraints
1 <= m <= 10000 <= n <= 1000
AI interview prep
Core dilemma
Efficiently counting combinatorial arrangements (permutations) that satisfy a specific property (exact number of inversions) within given constraints, avoiding exponential complexity.
2 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Count permutations with exactly n inversions
Count Permutations with Exactly N Inversions
You are given two integers m and n.
Consider all permutations of the array [1, 2, ..., m] (where each number from 1 to m appears exactly once). A permutation perm (0-indexed) has an inversion pair (x, y) if:
0 <= x < y < mperm[x] > perm[y]
A permutation is called special if it contains exactly n inversions.
Return the number of special permutations modulo 10^9 + 7.
Examples
- Input:
m = 3, n = 0Output:1Explanation: Only[1, 2, 3]has 0 inversions. - Input:
m = 3, n = 1Output:2Explanation:[1, 3, 2]and[2, 1, 3]each have exactly 1 inversion.
Constraints
1 <= m <= 10000 <= n <= 1000