Given an array, the task is to print or display all the reverse permutations of this array using STL in C++. Reverse permutation means, for an array {1, 2, 3}:
forward permutations: 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 reverse permutations: 3 2 1 3 1 2 2 3 1 2 1 3 1 3 2 1 2 3
Examples:
Input: a[] = {1, 2, 3}
Output:
3 2 1
3 1 2
2 3 1
2 1 3
1 3 2
1 2 3
Input: a[] = {10, 20, 30, 40}
Output:
Possible permutations are:
40 30 20 10
40 30 10 20
40 20 30 10
40 20 10 30
40 10 30 20
40 10 20 30
30 40 20 10
30 40 10 20
30 20 40 10
30 20 10 40
30 10 40 20
30 10 20 40
20 40 30 10
20 40 10 30
20 30 40 10
20 30 10 40
20 10 40 30
20 10 30 40
10 40 30 20
10 40 20 30
10 30 40 20
10 30 20 40
10 20 40 30
10 20 30 40
Approach: The next possible permutation of the array in reverse order can be found using prev_permutation() function provided in STL.
Syntax:
bool prev_permutation (BidirectionalIterator first,
BidirectionalIterator last);
Below is the implementation of the above Approach:
// C++ program to display all permutations // in reverse order // of an array using STL in C++ #include <bits/stdc++.h> using namespace std; // Function to display the array void display(int a[], int n) { for (int i = 0; i < n; i++) { cout << a[i] << " "; } cout << endl; } // Function to find the permutations void findPermutations(int a[], int n) { // Sort the given array sort(a, a + n); reverse(a, a + n); // Find all possible permutations cout << "Possible permutations are:\n"; do { display(a, n); } while (prev_permutation(a, a + n)); } // Driver code int main() { int a[] = { 10, 20, 30, 40 }; int n = sizeof(a) / sizeof(a[0]); findPermutations(a, n); return 0; } |
Possible permutations are: 40 30 20 10 40 30 10 20 40 20 30 10 40 20 10 30 40 10 30 20 40 10 20 30 30 40 20 10 30 40 10 20 30 20 40 10 30 20 10 40 30 10 40 20 30 10 20 40 20 40 30 10 20 40 10 30 20 30 40 10 20 30 10 40 20 10 40 30 20 10 30 40 10 40 30 20 10 40 20 30 10 30 40 20 10 30 20 40 10 20 40 30 10 20 30 40
Related Article: All permutations of an array using STL in C++
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- All permutations of an array using STL in C++
- How to reverse an Array using STL in C++?
- Reverse an array upto a given position
- Permutations of a given string using STL
- Problem on permutations and combinations | Set 2
- Print all permutations of a string in Java
- Check if two Linked Lists are permutations of each other
- Permutations to arrange N persons around a circular table
- Print all lexicographical greater permutations of a given string
- Number of permutations of a string in which all the occurrences of a given character occurs together
- std::reverse() in C++
- Number of permutations such that sum of elements at odd index and even index are equal
- How to reverse a Vector using STL in C++?
- forward_list::reverse() in C++ STL
- Reverse Middle X Characters
- Different methods to reverse a string in C/C++
- How to traverse a STL map in reverse direction?
- Reverse individual words
- How to traverse a C++ set in reverse direction
- list reverse function in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

