Next Permutation in linear time complexity

Illustration: 

Let’s try some examples to see if we can recognize some patterns. 

[3, 1, 3] = next greater number is 331
[5, 1, 3] = next greater number is 531
[1, 2, 3] = next greater number is 132
[1, 3, 5, 4] = next greater number is 1435
[3, 2, 1] = we can’t form a number greater than the current number from all the possible permutations

So, it is clear that to get the next permutation we will have to change the number in a position which is as right as possible. Each permutation (except the very first) has a increasing suffix. Now if we change the pattern from the pivot point (where the increasing suffix breaks) to its next possible lexicographic representation we will get the next greater permutation.

To understand how to change the pattern from pivot, see the below image:

Observation of Next permutation: 

Illustration of next_permutation

Follow the steps below to implement the above observation:

  • Iterate over the given array from end and find the first index (pivot) which doesn’t follow property of non-increasing suffix, (i.e,  arr[i] < arr[i + 1]).
  • Check if pivot index does not exist 
    • This means that the given sequence in the array is the largest as possible. So, swap the complete array.
  • Otherwise, Iterate the array from the end and find for the successor of pivot in suffix.
  • Swap the pivot and successor
  • Minimize the suffix part by reversing the array from pivot + 1 till N.

Below is the implementation of the above approach:



Output
1 2 4 3 5 6 


Time Complexity: O(N), where N is the size of the given array.
Auxiliary Space: O(1)



Next Permutation

Given an array arr[] of size N, the task is to print the lexicographically next greater permutation of the given array. If there does not exist any greater permutation, then print the lexicographically smallest permutation of the given array.

Examples:

Input: N = 6, arr = {1, 2, 3, 6, 5, 4}
Output: {1, 2, 4, 3, 5, 6}
Explanation: The next permutation of the given array is {1, 2, 4, 3, 5, 6}.

Input: N = 3, arr = {3, 2, 1}
Output: {1, 2, 3}
Explanation: As arr[] is the last permutation. 
So, the next permutation is the lowest one.

Let’s first understand what is lexicographical order in the above-given program.

We have to check that the order of the array sequence is greater than the previous array sequence. The output will be just larger sequence of the array.

Similar Reads

Brute Force Approach :

Find all possible permutations of the given array.Print the Next permutation right after the er given input sequence....

Using C++ in-build function:

C++ provides an in-built function called next_permutation(), that return directly lexicographically in the next greater permutation of the input....

Next Permutation in linear time complexity:

Illustration:...

Contact Us