forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNext_Permutation.py
More file actions
38 lines (31 loc) · 1.21 KB
/
Next_Permutation.py
File metadata and controls
38 lines (31 loc) · 1.21 KB
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
def nextPermutation(array):
"""
Function for next Permutation
It rearranges numbers into the lexicographically next greater permutation of numbers.
If such an arrangement is not possible, it will rearrange it as the lowest possible order (i.e., sorted in ascending order).
"""
length = len(array)
if length <= 2:
return array.reverse()
pointer = length - 2
while pointer >= 0 and array[pointer] >= array[pointer + 1]:
pointer -= 1
if pointer == -1:
return array.reverse()
for number in range(length - 1, pointer, -1):
if array[pointer] < array[number]:
array[pointer], array[number] = array[number], array[pointer]
break
array[pointer + 1:] = reversed(array[pointer + 1:])
return array
if __name__ == "__main__":
#User input for both the strings
print('Enter values of the array')
array = list(map(int, input().rstrip().split()))
print(nextPermutation(array))
"""
Input: numbers = [1,1,5]
Output: [1,5,1]
Input: numbers = [3,2,1]
Output: [1,2,3]
"""