Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.