VOOZH about

URL: https://www.geeksforgeeks.org/dsa/next-permutation/

⇱ Next Permutation - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Next Permutation

Last Updated : 25 Jun, 2025

Given an array of integers arr[] representing a permutation (i.e., all elements are unique and arranged in some order), find the next lexicographically greater permutation by rearranging the elements of the array.
If such a permutation does not exist (i.e., the array is the last possible permutation), rearrange the elements to form the lowest possible order (i.e., sorted in ascending order).

Examples:

Input: arr[] = [2, 4, 1, 7, 5, 0]
Output: [2, 4, 5, 0, 1, 7]
Explanation: The next lexicographically greater arrangement of the elements in the array arr[] is [2, 4, 5, 0, 1, 7].

Input: arr[] = [3, 2, 1]
Output: [1, 2, 3]
Explanation: This is the last permutation, so we return the lowest possible permutation (ascending order).

Input: arr[] = [1, 3, 5, 4, 2]
Output: [1, 4, 2, 3, 5]
Explanation: The next lexicographically greater arrangement of the elements in the array arr[] is [1, 4, 2, 3, 5].

[Naive Approach] Generate All Permutations - O(n! * n) Time and O(n! * n) Space

The idea is that we would first generate all possible permutations of a given array and sort them. Once sorted, we locate the current permutation within this list. The next permutation is simply the next arrangement in the sorted order. If the current arrangement is the last in the list then display the first permutation (smallest permutation).


Output
2 4 5 0 1 7 

[Expected Approach] Generating Only Next - O(n) Time and O(1) Space

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

[1, 2, 3, 4, 5]: next is [1, 2, 3, 5, 4]
Observation: 4 moves and 5 comes in place of it

[1, 2, 3, 5, 4]: next is [1, 2, 4, 3, 5]
Observation: 3 moves, 4 comes in place of it. 3 comes before 5 (mainly 3 and 5 are in sorted order)

[1, 2, 3, 6, 5, 4]: next is [1, 2, 4, 3, 5, 6]
Observation: 3 moves, 4 comes in place of it. [3, 5 and 6 are placed in sorted order]

[3, 2, 1]: next is [1, 2, 3]
Observation: All elements are reverse sorted. Result is whole array sorted.

Observations of Next permutation: 

  1. To get the next permutation we change the number in a position which is as right as possible.
  2. The first number to be moved is the rightmost number smaller than its next.
  3. The number to come in-place is the rightmost greater number on right side of the pivot.

Each permutation (except the very first) has an 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:


Step-By-Step Approach:

  • 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]).
  • If pivot index does not exist, then the given sequence in the array is the largest as possible. So, reverse the complete array. For example, for [3, 2, 1], the output would be [1, 2, 3]
  • Otherwise, Iterate the array from the end and find for the successor (rightmost greater element) of pivot in suffix.
  • Swap the pivot and successor
  • Minimize the suffix part by reversing the array from pivot + 1 till n.

Output
2 4 5 0 1 7 

[Alternate Approach in C++] Using Inbuilt Function - O(n) Time and O(1) Space

The idea is to use C++'s built-in function next_permutation(), which directly transforms the given array into its next lexicographically greater permutation. If the current arrangement is already the largest possible, this function rearranges the array into the smallest (sorted) permutation.


Output
2 4 5 0 1 7 
Comment
Article Tags: