Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-339

Permutation Rank

Problem

Return the zero-based lexicographic rank of a permutation of distinct integers.

For each position count how many unused smaller values remain to the right, multiply that count by the factorial of the remaining length, and sum the terms. The identity permutation has rank 0.

Hint

Each term counts the permutations that would have come before this choice at that position.

Test cases

case 1
in: [[1,2,3]]
expected: 0
case 2
in: [[3,2,1]]
expected: 5
case 3
in: [[2,1,3]]
expected: 2
case 4
in: [[1,3,2]]
expected: 1
case 5
in: [[2,3,1]]
expected: 3
Starter codePython
def permutation_rank(perm):
    # Your code here
    pass

5 test cases · run your code against them in the editor