Last active
February 9, 2019 23:50
-
-
Save horpto/7042f22d5b496226da15 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def NarayanaNextPerm(a): | |
n = len(a) | |
k = n - 2 | |
# step 1 | |
while a[k] >= a[k+1] && k >= 0: | |
k -= 1 | |
if k == -1: | |
return 0 | |
# step 2 | |
t = n - 1 | |
while a[k] >= a[t]: | |
t -= 1 | |
a[k], a[t] = a[t], a[k] | |
# step 3 | |
i, j = k + 1, n-1 | |
while i < j: | |
a[i], a[j] = a[j], [i] | |
i+= 1 | |
j-= 1 | |
return i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment