VOOZH about

URL: https://www.geeksforgeeks.org/dsa/number-of-permutation-with-k-inversions/

⇱ Number of permutation with K inversions - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Number of permutation with K inversions

Last Updated : 23 Jul, 2025

We are given two numbers n and k, the task is to find how many permutations of the first n number have exactly k inversion. Two elements in a sequence form an inversion if the smaller element appears before the larger element.

Examples:

Input: n = 3, k = 1
Output: 2
Explanation: Total Permutation of first 3 numbers are 123, 132, 213, 231, 312, 321
Permutation with 1 inversion: 132 and 213

Input: n = 3, k = 3
Output: 1
Explanation: Permutation with 3 inversions: 321

Using Recursion - O(n! * k) Time and O(n) Space

Base Cases:

  • If n == 0, no permutations are possible, so return 0.
  • If k == 0, there is exactly one permutation (the sorted order), so return 1.

Recursive Relation:

  • For each possible position i where the nth element can be placed (where i ranges from 0 to min(k, n-1)), we recursively call the function to calculate the number of permutations of n-1 elements with k-i inversions. This represents placing the nth element in a way that creates i inversions.

countPermWithkInversions(n, k) = Σ(countPermWithkInversions(n-1, k-i)) for i = 0 to min(k, n-1)


Output
5

Using Top-Down DP (Memoization) – O(n*k*k) Time and O(n*k) Space

The above recursive solution satisfies two key properties of Dynamic Programming, enabling us to use memoization for an optimized approach.

1. Optimal Substructure

We solve the problem of counting permutations with exactly k inversions by breaking it into subproblems involving fewer elements and inversions. Specifically, if we have n elements and k required inversions, we can compute it by trying positions for the largest element in a way that contributes to the inversion count.

The recursive relation is:

  • countPermWithkInversions(n, k) = Σ countPermWithkInversions(n-1, k-i)

Here, we consider placing the largest element in positions that create i inversions, where i ranges from 0 to min(k, n-1).

2. Overlapping Subproblems:

Many subproblems are computed multiple times when finding permutations for smaller subsets and inversion counts. For example, while calculating the result for (n, k), subproblems for (n-1, k-i) are encountered repeatedly.

We use a 2D array memo of size (n+1) x (k+1), initialized to -1, to store results of subproblems. If a result is already stored in memo[n][k], we return it directly, avoiding redundant calculations.


Output
5

Using Bottom-Up DP (Tabulation) – O(n*k*k) Time and O(n*k) Space

We create a 2D array dp of size (n + 1)*(k + 1), where the state dp[l][r] represents the number of permutations of l elements with exactly r inversions.

Base Case: dp[l][0] = 1 for all l, since there's only one way to have zero inversions-by arranging elements in sorted order.

For each element count l from 1 to n:

  • For each inversion count r from 1 to k, calculate the number of valid permutations.
  • To compute dp[l][r], we iterate over all possible positions of the largest element, which can create up to min(r, l-1) inversions, summing up the results from previous states

This relation fills the DP table by counting valid permutations based on subproblems:

  • dp[l][r] = sum of dp[l-1][r-i] for all valid i.

The final result is stored in dp[n][k], representing the total permutations of n elements with exactly k inversions.


Output
5

Time Complexity: O(n*k*k), where n is the number of elements and k is the number of inversions, due to the nested loops iterating through n, k, and possible inversions.
Auxiliary Space: O(n*k), because we maintain a 2D table of size (n+1)×(k+1) to store the computed values for dynamic programming.

Comment