Next Permutation

Problem Statement

Given an array 'arr', find the next lexicographical largest permutation within that array of integers

Example

Example-1: nums = [1,2,3] Output = [1,3,2] Possible permutations of [1,2,3] => [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] Assume a dictionary in that 123 will come first following by 132, 213, 231, 312, 321 Example-2: nums = [3,2,1] Ouput = [1,2,3] Same as above, it will again starts at 123

Approach

Code

def nextPermutation(arr): i = len(arr) - 2 while i >= 0 and arr[i] >= arr[i+1]: i -= 1 if i >= 0: j = len(arr) - 1 while j >= 0 and arr[j] <= arr[i]: j -= 1 arr[i], arr[j] = arr[j], arr[i] left, right = i + 1, len(arr) - 1 while left < right: arr[left], arr[right] = arr[right], arr[left] left += 1 right -= 1 return arr

Complexity Analysis