Skip to content

Instantly share code, notes, and snippets.

@prat0318
Last active December 28, 2015 13:39
Show Gist options
  • Save prat0318/7509212 to your computer and use it in GitHub Desktop.
Save prat0318/7509212 to your computer and use it in GitHub Desktop.
next greater in array
def next_greater(arr):
while(not is_rev_sorted(arr)):
for i in reversed(range(len(arr) - 1)):
if arr[i] < arr[i+1]: break
for j in reversed(range(len(arr))):
if arr[i] < arr[j]: break
swap(arr, i, j)
yield reverse(arr, i+1)
def swap(arr, i, j): arr[i], arr[j] = arr[j], arr[i]
def reverse(arr, i):
j = len(arr) - 1
while( i < j):
swap(arr, i, j)
i+=1; j-=1
return arr
def is_rev_sorted(arr):
rev=True
for i in range(len(arr) - 1):
if arr[i] < arr[i+1]:
rev=False
break
return rev
a = [1,2,3,4]
for i in next_greater(a):
print i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment