A relay squad of n runners is numbered 1 through n. A lineup is an ordering of all n runners. List every possible lineup in lexicographic (dictionary) order of the runner-number sequences and number them starting from 1. Given one particular lineup, output its position (rank) in that list.
Line 1: an integer n.
Line 2: n space-separated integers, a permutation of 1..n.
A single integer: the 1-indexed lexicographic rank of the given lineup.
Example 1
Input
3 2 3 1
Expected
4
Explanation
Order: 123,132,213,231,312,321. The lineup 231 is 4th, so the rank is 4.
Example 2
Input
1 1
Expected
1
Explanation
With one runner there is only one lineup, whose rank is 1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →