forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Next_premutation.py
56 lines (44 loc) · 1.26 KB
/
Next_premutation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'''
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order).
The replacement must be in place and use only constant extra memory.
Ex1:
Input :[1, 2, 3]
output:[1, 3, 2]
Ex2:
Input :[3, 2, 1]
output:[1, 2, 3]
Ex3:
Input :[1, 1, 5]
output:[1, 5, 1]
'''
def permute(arr):
#if the arr is smaller than 2 return the arr same
if len(arr)<2:
return arr
inverse=len(arr)-2
#if the array element start larger value then inverse the array
while inverse>=0 and arr[inverse]>=arr[inverse+1]:
inverse-=1
if inverse<0:
return
#checking the value larger value swap the poistion with smaller element
for i in reversed(range(inverse, len(arr))):
if arr[i]>arr[inverse]:
arr[i], arr[inverse]=arr[inverse],arr[i]
break
arr[inverse+1:]= reversed(arr[inverse+1:])
#result
return arr
if __name__ == "__main__":
arr= list(map(int, input("Enter the list: ").split()))
print(permute(arr))
'''
Time Complexcity: O(n)
Space Complexcity: O(1)
INPUT:
Enter the list:
1 2 3
OUTPUT:
[1,3,2]
'''