
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
All reverse permutations of an array using STL in C++?
In this section we will see how to generate all reverse permutations using the STL in C++. The forward and reverse permutation of some numbers like (1, 2, 3) will be like below −
Forward permutation
1, 2, 3 1, 3, 2 2, 1, 3 2, 3, 1 3, 1, 2 3, 2, 1
Reversed permutation
3, 2, 1 3, 1, 2 2, 3, 1 2, 1, 3 1, 3, 2 1, 2, 3
We will use the previous_permutation() function to get the result
Algorithm
getPermutation(arr, n)
Begin sort arr reverse the arr repeat print array elements until the previous permutation calculation is not completed End
Example
#include<iostream> #include <algorithm> using namespace std; void disp(int arr[], int n){ for(int i = 0; i<n; i++){ cout << arr[i] << " "; } cout << endl; } void getPermutation(int arr[], int n) { sort(arr, arr + n); reverse(arr, arr+n); cout << "Possible permutations: \n"; do{ disp(arr, n); }while(prev_permutation(arr, arr+n)); } int main() { int arr[] = {11, 22, 33, 44}; int n = sizeof(arr) / sizeof(arr[0]); getPermutation(arr, n); }
Output
Possible permutations: 44 33 22 11 44 33 11 22 44 22 33 11 44 22 11 33 44 11 33 22 44 11 22 33 33 44 22 11 33 44 11 22 33 22 44 11 33 22 11 44 33 11 44 22 33 11 22 44 22 44 33 11 22 44 11 33 22 33 44 11 22 33 11 44 22 11 44 33 22 11 33 44 11 44 33 22 11 44 22 33 11 33 44 22 11 33 22 44 11 22 44 33 11 22 33 44
- Related Articles
- How to reverse an Array using STL in C++?
- C++ Permutations of a Given String Using STL
- Take an array of integers and create an array of all the possible permutations in JavaScript
- Generating all possible permutations of array in JavaScript
- Reverse an array using C#
- All permutations of a string using iteration?
- Shuffle an Array using STL in C++
- How to reverse a Vector using STL in C++?
- How to sort an Array using STL in C++?
- How to reverse the elements of an array using stack in java?
- Reverse an array in C++
- forward_list::reverse( ) in C++ STL
- Sorting an array according to another array using pair in STL in C++
- Reverse digits of an integer in JavaScript without using array or string methods
- Rank of All Elements in an Array using C++

Advertisements