Skip to content

Instantly share code, notes, and snippets.

@horpto
Last active February 9, 2019 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save horpto/7042f22d5b496226da15 to your computer and use it in GitHub Desktop.
Save horpto/7042f22d5b496226da15 to your computer and use it in GitHub Desktop.
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