std::next_permutation and prev_permutation in C++
Last Updated :
07 Oct, 2024
For a given collection of N elements, a permutation is N! (factorial) possible arrangements the elements. Different permutations can be ordered according to how they compare lexicographically to each other.
In C++, the std::next_permutation() and std::prev_permutation() functions are used to rearrange the elements of container in lexicographically larger and smaller permutation of the given range respectively. They are defined inside the <algorithm> header file.
In this article, we will learn how to use the next_permuatation() and prev_permutation() in C++
std::next_permutation()
The std::next_permutation in C++ is used to rearrange the elements of the given range [first, last) to the lexicographical larger permutation if it exists.
Syntax
std::next_permutation(first, last);
Parameters
- first: Iterator to the first element of the given range.
- last: Iterator to the theoretical element just after the last element of the given range.
Return Value
- Returns true if the container could be rearranged to the to the lexicographical larger permutation.
- Returns false otherwise.
Example of std::next_permutation()
C++
// C++ program to demonstate the use of
// std::next_permutation() function
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 2, 3};
// Printing all the greater permutations
// of the current vector
do {
for (auto i: v) cout << i << " ";
cout << endl;
} while (next_permutation(v.begin(), v.end()));
return 0;
}
Output1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
Time Complexity: O(n), for each permutation where n is the number of elements in the range.
Auxiliary Space: O(1)
Explanation: The total number of permutations of vector of 3 elements is 3! = 6. We already took the smallest possible permutation as the starting point, so we were able to print all the permutations using next_permutation().
std::prev_permutation()
The std::prev_permutation is used to rearrange the elements of the given range [first, last) in the lexicographical smaller permutation if it exists.
Syntax
std::prev_permutataion(first, last)
Parameters
- first: Iterator to the first element of the given range.
- last: Iterator to the theoretical element just after the last element of the given range.
Return Value
- Returns true if the container could be rearranged to the to the lexicographical smaller permutation.
- Returns false otherwise.
Example of std::prev_permutation()
C++
// C++ program to demonstate the use of
// std::next_permutation() function
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {2, 1, 3};
// Printing all the possible permutations
// smaller than the current one
do {
for (auto i: v) cout << i << " ";
cout << endl;
} while (prev_permutation(v.begin(), v.end()));
return 0;
}
Time Complexity: O(n), for each permutation where n is the number of elements in the range.
Auxiliary Space: O(1)
Explanation: The total number of permutations of vector of 3 elements is 3! = 6. But we were only able to print 3 permutations because we didn't took the largest permutation as starting point for prev_permutation() function. So, all the permutation greater than the permutation {2, 1, 3} are left out.
Similar Reads
Number of Transpositions in a Permutation Cycle notation is a compact way to represent a permutation by breaking it down into cycles. A cycle represents a set of elements that are permuted (or swapped) among each other.Examples:Let us consider the permutation p = [5, 1, 4, 2, 3] of [1, 2, 3, 4, 5], the elements are moved as 1 â 5, 5 â 3, 3
6 min read
Missing Permutations in a list Given a list of permutations of any word. Find the missing permutation from the list of permutations. Examples: Input : Permutation_given[] = {"ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD", "ADCB", "CDAB", "DABC", "BCAD", "CADB", "CDBA", "CBAD", "ABDC", "ADBC", "BDCA", "DCBA", "BACD", "BADC", "BDAC
6 min read
std::is_permutation in C++ STL The C++ function std::algorithm::is_permutation() tests whether a sequence is permutation of other or not. It uses operator == for comparison. This function was defined in C++11.Syntax:template <class ForwardIterator1, class ForwardIterator2 >bool is_permutation(ForwardIterator1 first1, Forwar
3 min read
Generate permutations with only adjacent swaps allowed Given a string on length N. You can swap only the adjacent elements and each element can be swapped atmost once. Find the no of permutations of the string that can be generated after performing the swaps as mentioned. Examples: Input : 12345 Output : 12345 12354 12435 13245 13254 21345 21354 21435 S
5 min read
Permutations of a given string using STL Given a string s, the task is to return all unique permutations of a given string in lexicographically sorted order.Note: A permutation is the rearrangement of all the elements of a string.Examples:Input: s = "ABC"Output: "ABC", "ACB", "BAC", "BCA", "CBA", "CAB"Input: s = "XY"Output: "XY", "YX"Input
4 min read