All reverse permutations of an array using STL in C++ Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: CPP // 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; } 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 Related Article: All permutations of an array using STL in C++ Comment More infoAdvertise with us Next Article Iterative program to generate distinct Permutations of a String C code_r Follow Improve Article Tags : C++ Programs C++ STL cpp-array Permutation and Combination +1 More Practice Tags : CPPSTL Similar Reads Find All Permutations of an Array using STL in C++ The permutation of an array refers to a rearrangement of its elements in every possible order. In this article, we will learn how to generate all possible permutation of an array using STL in C++.The simplest method to find all the permutations of an array is to use next_permutation(). The array has 2 min read How to Reverse an Array using STL in C++? Reversing an array means rearranging its elements so that the first element becomes the last, the second element becomes the second last, and so on. In this article, we will learn how to reverse an array using STL in C++.The most efficient way to reverse an array using STL is by using reverse() func 2 min read Sort permutation of N natural numbers using triple cyclic right swaps Given an array arr[] of size N which contains the permutations of the N natural numbers, the task is to sort the permutations of N natural numbers with the help of triple cyclic right swaps. Triple Cyclic Right Swaps: refers to the triple cyclic right shift in which - arr[i] -> arr[j] -> arr[k 11 min read Working with Array and Vectors using STL in C++ Using STL library it is very easy to perform certain basic operations on array like Sorting, searching, sum of elements, finding minimum and maximum element of the array. Sorting Sorting can be done with the help of sort() function. sort(starting_index, last_index) â To sort the given array/vector. 6 min read Iterative program to generate distinct Permutations of a String Given a string str, the task is to generate all the distinct permutations of the given string iteratively. Examples: Input: str = "bba" Output: abb bab bba Input: str = "abc" Output: abc acb bac bca cab cba Approach: The number of permutations for a string of length n are n!. The following algorithm 15+ min read C++ Program for Minimum move to end operations to make all strings equal Given n strings that are permutations of each other. We need to make all strings same with an operation that takes front character of any string and moves it to the end.Examples: Input : n = 2 arr[] = {"molzv", "lzvmo"} Output : 2 Explanation: In first string, we remove first element("m") from first 3 min read Like