human
Number of permutation with K inversions | Set 2 | Function to count permutations with K inversions.
Store number of permutations with K inversions.
If N = 1 only 1 permutation with no inversion.
For K = 0 only 1 permutation with no inversion.
Otherwise Update each dp state as per the reccurrance relation formed.
Print final count.
Driver Code.
Given N and K.
Function Call"
How can the above be solved in Python?
assistant
#Here is code to number of permutation with K inversions and the other steps required.
 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 )