Jayakrishna14

Rank of a Permutation

Aug 10th, 2025 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.37 KB | None | 0 0
  1. from math import factorial
  2.  
  3. def find_rank(s):
  4.     n = len(s)
  5.     rank = 0
  6.     chars = list(s)
  7.  
  8.     for i in range(n):
  9.         count = 0
  10.         for j in range(i + 1, n):
  11.             if chars[j] < chars[i]:
  12.                 count += 1
  13.         rank += count * factorial(n - i - 1)
  14.    
  15.     return rank + 1
  16.  
  17. # Test Case
  18. print(find_rank("123456798"))  # Output: 2
Tags: permutation
Advertisement
Add Comment
Please, Sign In to add comment