Next - Permutation - Find Next Lexicographically Greater Permutation - Arrays - Tutorial
Next - Permutation - Find Next Lexicographically Greater Permutation - Arrays - Tutorial
takeUforward
lexicographically greater
permutation
Problem Statement: Given an array Arr[] of integers, rearrange the numbers
of the given array into the lexicographically next greater permutation of
numbers.
Examples
https://takeuforward.org/data-structure/next_permutation-find-next-lexicographically-greater-permutation/ 1/12
7/9/23, 3:41 PM next_permutation : find next lexicographically greater permutation - Arrays - Tutorial
Disclaimer: Don’t jump directly to the solution, try it out yourself first.
Solution 1
Solution 2
Code
#include<iostream>
#include<vector>
#include<algorithm>
https://takeuforward.org/data-structure/next_permutation-find-next-lexicographically-greater-permutation/ 2/12
7/9/23, 3:41 PM next_permutation : find next lexicographically greater permutation - Arrays - Tutorial
return 0;
}
Latest Video on
Output: The next permutation is: 2 1 3
Striver
Algorithm / Intuition
https://takeuforward.org/data-structure/next_permutation-find-next-lexicographically-greater-permutation/ 3/12
7/9/23, 3:41 PM next_permutation : find next lexicographically greater permutation - Arrays - Tutorial
1. Find the break-point, i: Break-point means the first index i from the
back of the given array where arr[i] becomes smaller than arr[i+1].
For example, if the given array is {2,1,5,4,3,0,0}, the break-point will
be index 1(0-based indexing). Here from the back of the array, index
1 is the first index where arr[1] i.e. 1 is smaller than arr[i+1] i.e. 5.
To find the break-point, using a loop we will traverse the array backward
and store the index i where arr[i] is less than the value at index (i+1) i.e.
arr[i+1].
2. If such a break-point does not exist i.e. if the array is sorted in
decreasing order, the given permutation is the last one in the
sorted order of all possible permutations. So, the next permutation
must be the first i.e. the permutation in increasing order.
So, in this case, we will reverse the whole array and will return it as
our answer.
3. If a break-point exists:
1. Find the smallest number i.e. > arr[i] and in the right half of
index i(i.e. from index i+1 to n-1) and swap it with arr[i].
2. Reverse the entire right half(i.e. from index i+1 to n-1) of
index i. And finally, return the array.
Recent Posts
Kth Missing Positive Number
https://takeuforward.org/data-structure/next_permutation-find-next-lexicographically-greater-permutation/ 4/12
7/9/23, 3:41 PM next_permutation : find next lexicographically greater permutation - Arrays - Tutorial
Note: For a better understanding of intuition, please watch the video at Capacity to Ship Packages
the bottom of the page. within D Days
We build up the intuition of the algorithm through the following Minimum days to make M
observations. bouquets
Observation 1
Solution
Striver’s SDE 1 “raj”A2Z
Sheet Striver’s
For example, Solution
and 2 a common
DSA Course/Sheet
“rax” has Solution 3 “ra”
Striver’s DSA
prefix i.e.
Playlists CS Subjects Interview Prep Sheets Striver’s CP Sheet
and
the differentiating characters are ‘j’ and ‘x’. Now, as ‘j’ appears
before ‘x’ in the alphabet, “raj” appears before “rax” in the given
order. The same logic is applicable for “rax” and “rbx”(Common
prefix: “r”, differentiating characters: ‘a’ and ‘b’).
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
We can call this index i as the break-point of the array. The left
half of index i (the length of the left half might be 0) in the current
permutation is the same as in the previous permutation. And
the right half of the break-point is always in decreasing order.
https://takeuforward.org/data-structure/next_permutation-find-next-lexicographically-greater-permutation/ 6/12
7/9/23, 3:41 PM next_permutation : find next lexicographically greater permutation - Arrays - Tutorial
https://takeuforward.org/data-structure/next_permutation-find-next-lexicographically-greater-permutation/ 7/12
7/9/23, 3:41 PM next_permutation : find next lexicographically greater permutation - Arrays - Tutorial
Observation 2
Observation 3
Observation 4
Dry Run
Code
https://takeuforward.org/data-structure/next_permutation-find-next-lexicographically-greater-permutation/ 8/12
7/9/23, 3:41 PM next_permutation : find next lexicographically greater permutation - Arrays - Tutorial
#include <bits/stdc++.h>
using namespace std;
https://takeuforward.org/data-structure/next_permutation-find-next-lexicographically-greater-permutation/ 9/12
7/9/23, 3:41 PM next_permutation : find next lexicographically greater permutation - Arrays - Tutorial
return A;
}
int main()
{
vector<int> A = {2, 1, 5, 4, 3, 0, 0};
vector<int> ans = nextGreaterPermutation(A);
https://takeuforward.org/data-structure/next_permutation-find-next-lexicographically-greater-permutation/ 10/12
7/9/23, 3:41 PM next_permutation : find next lexicographically greater permutation - Arrays - Tutorial
Complexity Analysis
Video Explanation
Load Comments
https://takeuforward.org/data-structure/next_permutation-find-next-lexicographically-greater-permutation/ 11/12
7/9/23, 3:41 PM next_permutation : find next lexicographically greater permutation - Arrays - Tutorial
https://takeuforward.org/data-structure/next_permutation-find-next-lexicographically-greater-permutation/ 12/12