human
Difference between lexicographical ranks of two given permutations | Function the print the difference between the lexicographical ranks.
Store the permutations in lexicographic order.
Initial permutation.
Initial variables.
Check permutation.
Initialize second permutation.
Check permutation.
Print difference.
Given array P [ ].
Given array Q [ ].
Given size.
Function call"
Solution in Python:
 ... Write me a code to difference between lexicographical ranks of two given permutations and the other steps required?
assistant
#Here is code to difference between lexicographical ranks of two given permutations and the other steps required.
 def findDifference ( p , q , N ) :
	A = [ 0 ] * N
	for i in range ( N ) :
		A [ i ] = i + 1
	IsCorrect = False
	a , b = 1 , 1
	while True :
		IsCorrect = True
		for i in range ( N ) :
			if ( A [ i ] != p [ i ] ) :
				IsCorrect = False
				break
		if ( IsCorrect ) :
			break
		a += 1
		if ( not next_permutation ( A ) ) :
			break
	for i in range ( N ) :
		A [ i ] = i + 1
	while True :
		IsCorrect = True
		for i in range ( N ) :
			if ( A [ i ] != q [ i ] ) :
				IsCorrect = False
				break
		if ( IsCorrect ) :
			break
		b += 1
		if ( not next_permutation ( A ) ) :
			break
	print ( abs ( a - b ) )
def next_permutation ( p ) :
	for a in range ( len ( p ) - 2 , - 1 , - 1 ) :
		if ( p [ a ] < p [ a + 1 ] ) :
			b = len ( p ) - 1
			while True :
				if ( p [ b ] > p [ a ] ) :
					t = p [ a ]
					p [ a ] = p [ b ]
					p [ b ] = t
					a += 1
					b = len ( p ) - 1
					while a < b :
						t = p [ a ]
						p [ a ] = p [ b ]
						p [ b ] = t
						a += 1
						b -= 1
					return True
				b -= 1
	return False
p = [ 1 , 3 , 2 ]
q = [ 3 , 1 , 2 ]
n = len ( p )
findDifference ( p , q , n )