Problem: We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).  There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Submission attempts (in order):
def permutation(lst): 
    # If lst is empty then there are no permutations 
	if len(lst) == 0: 
		return [] 
  
    # If there is only one element in lst then, only 
    # one permuatation is possible 
	if len(lst) == 1: 
		return [lst] 
  
    # Find the permutations for lst if there are 
    # more than 1 characters 
  
    l = [] # empty list that will store current permutation 
  
    # Iterate the input(lst) and calculate the permutation 
    for i in range(len(lst)): 
       m = lst[i] 
  
       # Extract lst[i] or m from the list.  remLst is 
       # remaining list 
       remLst = lst[:i] + lst[i+1:] 
  
       # Generating all permutations where m is first 
       # element 
       for p in permutation(remLst): 
           l.append([m] + p) 
    return l 
 
N = input()
x = [int(x) for x in input().split()]
y = [int(x) for x in input().split()]
 
NN = list(range(1,int(N)+1))
P = permutation(NN)
 
xx = P.index(x)
yy = P.index(y)
print(abs(xx-yy))

def permutation(lst): 
  
    # If lst is empty then there are no permutations 
    if len(lst) == 0: 
        return [] 
  
    # If there is only one element in lst then, only 
    # one permuatation is possible 
    if len(lst) == 1: 
        return [lst] 
  
    # Find the permutations for lst if there are 
    # more than 1 characters 
  
    l = [] # empty list that will store current permutation 
  
    # Iterate the input(lst) and calculate the permutation 
	for i in range(len(lst)): 
		m = lst[i] 
  
       # Extract lst[i] or m from the list.  remLst is 
       # remaining list 
       remLst = lst[:i] + lst[i+1:] 
  
       # Generating all permutations where m is first 
       # element 
	for p in permutation(remLst): 
           l.append([m] + p) 
    return l 

N= input()
x = [int(x) for x in input().split()]
y = [int(x) for x in input().split()]
 
NN = list(range(1,int(N)+1))
P = permutation(NN)
 
xx = P.index(x)
yy = P.index(y)
print(abs(xx-yy))

def permutation(lst):
    # If lst is empty then there are no permutations
  if len(lst) == 0:
    return []

        # If there is only one element in lst then, only
    # one permuatation is possible
  if len(lst) == 1:
    return [lst]

        # Find the permutations for lst if there are
    # more than 1 characters

  l = []  # empty list that will store current permutation

    # Iterate the input(lst) and calculate the permutation
  for i in range(len(lst)):
    m = lst[i]

        # Extract lst[i] or m from the list.  remLst is
        # remaining list
    remLst = lst[:i] + lst[i + 1:]

        # Generating all permutations where m is first
        # element
    for p in permutation(remLst):
      l.append([m] + p)
  return l


N = input()
x = [int(x) for x in input().split()]
y = [int(x) for x in input().split()]

NN = list(range(1, int(N) + 1))
P = permutation(NN)

xx = P.index(x)
yy = P.index(y)
print(abs(xx - yy))

Verdicts: Runtime Error

Runtime Error

Accepted