Find All Permutations of an Array using STL in C++ Last Updated : 15 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice 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 to be sorted in ascending order because this function generate the next lexicographically larger permutation of an array. The below example shows how to implement this method: C++ #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1, 3, 2}; int n = sizeof(arr) / sizeof(arr[0]); sort(arr, arr + n); // Generate all permuatation of an array do { for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; } while (next_permutation(arr, arr + n)); return 0; } Output1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 Explanation: The next_permutation() method returns true if the next permutation exists and rearranges the arr to it and returns false if it doesn't exits. So, we used a loop in the above program that runs till next_permutation() returns false.Note: This function modifies the given array to generate its next permutation.Using prev_permutation() FunctionThe prev_permutation() does just the opposite of the prev_permutation() function and generate all the previous permutation of the given array. But the given array should be sorted in the descending order. C++ #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1, 3, 2}; int n = sizeof(arr) / sizeof(arr[0]); sort(arr, arr + n, greater<int>()); // Generate all permuatation of an array do { for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; } while (prev_permutation(arr, arr + n)); return 0; } Output3 2 1 3 1 2 2 3 1 2 1 3 1 3 2 1 2 3 Comment More infoAdvertise with us Next Article Find All Permutations of an Array using STL in C++ C code_r Follow Improve Article Tags : C++ Programs C++ STL cpp-array Permutation and Combination CPP Examples +2 More Practice Tags : CPPSTL Similar Reads All reverse permutations of an array using STL in C++ 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[] = { 3 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 How to find the sum of elements of an Array using STL in C++? Given an array arr[], find the sum of the elements of this array using STL in C++. Example: Input: arr[] = {1, 45, 54, 71, 76, 12} Output: 259 Input: arr[] = {1, 7, 5, 4, 6, 12} Output: 35 Approach: Sum can be found with the help of accumulate() function provided in STL. Syntax: accumulate(first_ind 1 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 How to Sort an Array in Descending Order using STL in C++? Sort an array in descending order means arranging the elements in such a way that the largest element at first place, second largest at second place and so on. In this article, we will learn how to sort an array in descending order using STL in C++. ExamplesInput: arr[] = {11, 9, 45, 21};Output: 78 4 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 Generate all possible permutations of a Number divisible by N Given a numerical string S, the task is to print all the permutations of the string which are divisible by N. Examples: Input: N = 5, S = "125" Output: 125 215Explanation: All possible permutations are S are {125, 152, 215, 251, 521, 512}. Out of these 6 permutations, only 2 {125, 215} are divisible 5 min read Count of distinct permutations of every possible length of given string Given a string S, the task is to count the distinct permutations of every possible length of the given string. Note: Repetition of characters is not allowed in the string. Input: S = âabcâOutput: 15Explanation:Possible Permutations of every length are:{âaâ, âbâ, âcâ, âabâ, âbcâ, âacâ, âbaâ, âcaâ, âc 5 min read How to Find All Indexes of an Element in an Array in C++? In C++, an array is a collection of elements of the same type placed in contiguous memory locations. In this article, we will learn how to find all indexes of a specific element in an array in C++. Example: Input: myArray = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4}; target = 3Output: The element 3 occurred at 2 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 Like