Number of permutation with K inversions | Set 2
Last Updated :
24 Mar, 2023
Given two integers N and K, the task is to count the number of permutations of the first N natural numbers having exactly K inversions. Since the count can be very large, print it modulo 109 + 7.
An inversion is defined as a pair a[i], a[j] such that a[i] > a[j] and i < j.
Examples:
Input: N = 3, K = 2
Output: 2
Explanation:
All Permutations for N = 3 are 321, 231, 213, 312, 132, 123.
Out of which only 231 and 312 have 2 inversions as:
- 231: 2 > 1 & 3 > 1
- 312: 3 > 1 & 3 > 2.
Therefore, both are satisfying the condition of having exactly K inversions.
Input: N = 5, K = 5
Output: 22
Naive Approach: Refer to the previous post for the simplest possible approach to solve the problem.
Time Complexity: O(N*N!)
Auxiliary Space: O(1)
Dynamic Programming using Top-Down Approach: Refer to the previous post of this article for the Top-Down Approach.
Time Complexity: O(N*K2)
Auxiliary Space: O(N*K)
Dynamic Programming using Bottom-Up Approach:
Illustration:
For Example: N = 4, K = 2
N – 1 = 3, K0 = 0 … 123 => 1423
N – 1 = 3, K1 = 1 … 213, 132 => 2143, 1342
N – 1 = 3, K2 = 2 … 231, 312 => 2314, 3124
So the answer is 5.
The maximum value is taken between (K – N + 1) and 0 as K inversions cannot be obtained if the number of inversions in permutation of (N – 1) numbers is less than K – (N – 1) as at most (N – 1) new inversions can be obtained by adding Nth number at the beginning.
Follow the steps below to solve the problem:
- Create an auxiliary array dp[2][K + 1] where dp[N][K] stores all permutations of (N – 1) numbers with K = (max(K – (N – 1), 0) to K) inversions, by adding Nth number with them only once.
- Using dp[i % 2][K] will interchange iteration between two rows and take j = Max(K – (N – 1), 0). So dp[N[K] = dp[N-1][j] + dp[N-1][j+1] + …. + dp[N – 1][K].
- For calculating dp[N][K] there is no need to do this extra K iteration as it can be obtained in O(1) from dp[N][K – 1]. So the recurrence relation is given by:
- dp[N][K] = dp[N][K – 1] + dp[N – 1][K] – dp[N – 1][max(K – (N – 1), 0) – 1]
- Iterate two nested loops using the variable i and j over N and K respectively and update each dp states as per the above recurrence relation.
- Print the value of dp[N%2][K] after the above steps as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int numberOfPermWithKInversion(
int N, int K)
{
int dp[2][K + 1];
int mod = 1000000007;
for ( int i = 1; i <= N; i++) {
for ( int j = 0; j <= K; j++) {
if (i == 1)
dp[i % 2][j] = (j == 0);
else if (j == 0)
dp[i % 2][j] = 1;
else
dp[i % 2][j]
= (dp[i % 2][j - 1] % mod
+ (dp[1 - i % 2][j]
- ((max(j - (i - 1), 0) == 0)
? 0
: dp[1 - i % 2]
[max(j - (i - 1), 0)
- 1])
+ mod)
% mod)
% mod;
;
}
}
cout << dp[N % 2][K];
}
int main()
{
int N = 3, K = 2;
numberOfPermWithKInversion(N, K);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void numberOfPermWithKInversion( int N, int K)
{
int [][] dp = new int [ 2 ][K + 1 ];
int mod = 1000000007 ;
for ( int i = 1 ; i <= N; i++)
{
for ( int j = 0 ; j <= K; j++)
{
if (i == 1 )
{
dp[i % 2 ][j] = (j == 0 ) ? 1 : 0 ;
}
else if (j == 0 )
dp[i % 2 ][j] = 1 ;
else
{
int maxm = Math.max(j - (i - 1 ));
dp[i % 2 ][j] = (dp[i % 2 ][j - 1 ] % mod +
(dp[ 1 - i % 2 ][j] -
((Math.max(j - (i - 1 ), 0 ) == 0 ) ?
0 : dp[ 1 - i % 2 ][maxm, 0 ) - 1 ]) +
mod) % mod) % mod;
}
}
}
System.out.println (dp[N % 2 ][K]);
}
public static void main(String[] args)
{
int N = 3 , K = 2 ;
numberOfPermWithKInversion(N, K);
}
}
|
Python3
def numberOfPermWithKInversion(N, K):
dp = [[ 0 ] * (K + 1 )] * 2
mod = 1000000007
for i in range ( 1 , N + 1 ):
for j in range ( 0 , K + 1 ):
if (i = = 1 ):
dp[i % 2 ][j] = 1 if (j = = 0 ) else 0
elif (j = = 0 ):
dp[i % 2 ][j] = 1
else :
var = ( 0 if ( max (j - (i - 1 ), 0 ) = = 0 )
else dp[ 1 - i % 2 ][ max (j - (i - 1 ), 0 ) - 1 ])
dp[i % 2 ][j] = ((dp[i % 2 ][j - 1 ] % mod +
(dp[ 1 - i % 2 ][j] -
(var) + mod) % mod) % mod)
print (dp[N % 2 ][K])
if __name__ = = '__main__' :
N = 3
K = 2
numberOfPermWithKInversion(N, K)
|
C#
using System;
class GFG{
static void numberOfPermWithKInversion( int N, int K)
{
int [,] dp = new int [2, K + 1];
int mod = 1000000007;
for ( int i = 1; i <= N; i++)
{
for ( int j = 0; j <= K; j++)
{
if (i == 1)
{
dp[i % 2, j] = (j == 0) ? 1 : 0;
}
else if (j == 0)
dp[i % 2, j] = 1;
else
dp[i % 2, j] = (dp[i % 2, j - 1] % mod +
(dp[1 - i % 2, j] -
((Math.Max(j - (i - 1), 0) == 0) ?
0 : dp[1 - i % 2, Math.Max(
j - (i - 1), 0) - 1]) +
mod) % mod) % mod;
}
}
Console.WriteLine(dp[N % 2, K]);
}
public static void Main()
{
int N = 3, K = 2;
numberOfPermWithKInversion(N, K);
}
}
|
Javascript
<script>
function numberOfPermWithKInversion(N, K)
{
let dp = new Array(2);
for ( var i = 0; i < dp.length; i++)
{
dp[i] = new Array(2);
}
let mod = 1000000007;
for (let i = 1; i <= N; i++)
{
for (let j = 0; j <= K; j++)
{
if (i == 1)
{
dp[i % 2][j] = (j == 0) ? 1 : 0;
}
else if (j == 0)
dp[i % 2][j] = 1;
else
dp[i % 2][j] = (dp[i % 2][j - 1] % mod +
(dp[1 - i % 2][j] -
((Math.max(j - (i - 1), 0) == 0) ?
0 : dp[1 - i % 2][(Math.max(j -
(i - 1), 0) - 1)]) +
mod) % mod) % mod;
}
}
document.write(dp[N % 2][K]);
}
let N = 3, K = 2;
numberOfPermWithKInversion(N, K);
</script>
|
Time Complexity: O(N * K)
Auxiliary Space: O(K)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...