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 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 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 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 How to Sort an Array in C++? Sorting an array involves rearranging its elements in a specific order such as from smallest to largest element or from largest to smallest element, etc. In this article, we will learn how to sort an array in C++.Example:Input: arr ={5,4,1,2,3}Output: 1 2 3 4 5Explanation: arr is sorted in increasin 4 min read How to Find the Unique Elements in an Array in C++? In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to find the unique elements in an array in C++. Example: Input: array = {1, 2, 1, 2, 2, 3, 4} Output: Unique elements in the array: 1 2 2 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