# std::next_permutation and prev_permutation in C++

**std::next_permutation**

It is used to rearrange the elements in the range [first, last) into the next lexicographically greater permutation. A permutation is each one of the N! possible arrangements the elements can take (where N is the number of elements in the range). Different permutations can be ordered according to how they compare lexicographically to each other.

**Syntax: **

template bool next_permutation (BidirectionalIterator first, BidirectionalIterator last);Parameters:first, last : Bidirectional iterators to the initial and final positions of the sequence. The range used is [first, last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.return value:true :if the function could rearrange the object as a lexicographicaly greater permutation. Otherwise, the function returns false to indicate that the arrangementis not greater than the previous, but the lowest possible (sorted in ascending order).

**Application : **next_permutation is to find next lexicographicaly greater value for given array of values.

Examples:

Input : next permutation of 1 2 3 is Output : 1 3 2 Input : next permutation of 4 6 8 is Output : 4 8 6

`// C++ program to illustrate ` `// next_permutation example ` ` ` `// this header file contains next_permutation function ` `#include <algorithm> ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 2, 3 }; ` ` ` ` ` `sort(arr, arr + 3); ` ` ` ` ` `cout << ` `"The 3! possible permutations with 3 elements:\n"` `; ` ` ` `do` `{ ` ` ` `cout << arr[0] << ` `" "` `<< arr[1] << ` `" "` `<< arr[2] << ` `"\n"` `; ` ` ` `} ` `while` `(next_permutation(arr, arr + 3)); ` ` ` ` ` `cout << ` `"After loop: "` `<< arr[0] << ` `' '` ` ` `<< arr[1] << ` `' '` `<< arr[2] << ` `'\n'` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

The 3! possible permutations with 3 elements: 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 After loop: 1 2 3

**std::prev_permutation **

It is used to rearranges the elements in the range [first, last) into the previous lexicographically-ordered permutation. A permutation is each one of the N! possible arrangements the elements can take (where N is the number of elements in the range). Different permutations can be ordered according to how they compare lexicographicaly to each other.

**Syntax : **

template bool prev_permutation (BidirectionalIterator first, BidirectionalIterator last );parameters:first, last : Bidirectional iterators to the initial and final positions of the sequence. The range used is [first, last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.return value:true :if the function could rearrange the object as a lexicographicaly smaller permutation. Otherwise, the function returns false to indicate that the arrangement is not less than the previous, but the largest possible (sorted in descending order).

**Application : ** prev_permutation is to find previous lexicographicaly smaller value for given array of values.

Examples:

Input : prev permutation of 3 2 1 is Output : 3 1 2 Input : prev permutation of 8 6 4 is Output :8 4 6

`// C++ program to illustrate ` `// prev_permutation example ` ` ` `// this header file contains prev_permutation function ` `#include <algorithm> ` ` ` `#include <iostream> ` `using` `namespace` `std; ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 2, 3 }; ` ` ` ` ` `sort(arr, arr + 3); ` ` ` `reverse(arr, arr + 3); ` ` ` ` ` `cout << ` `"The 3! possible permutations with 3 elements:\n"` `; ` ` ` `do` `{ ` ` ` `cout << arr[0] << ` `" "` `<< arr[1] << ` `" "` `<< arr[2] << ` `"\n"` `; ` ` ` `} ` `while` `(prev_permutation(arr, arr + 3)); ` ` ` ` ` `cout << ` `"After loop: "` `<< arr[0] << ` `' '` `<< arr[1] ` ` ` `<< ` `' '` `<< arr[2] << ` `'\n'` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

The 3! possible permutations with 3 elements: 3 2 1 3 1 2 2 3 1 2 1 3 1 3 2 1 2 3 After loop: 3 2 1

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Difference between cout and puts() in C++ with Examples
- Difference between forward list and list in C++
- Short-Circuiting in C++ and Linux
- Different types of range-based for loop iterators in C++
- Learn C++ Programming Step by Step - A 20 Day Curriculum!
- Check if Array elements can be maximized upto M by adding all elements from another array
- Why do we need reference variables if we have pointers
- Similarities and Difference between Java and C++
- Macros and its types in C/C++
- Reversed Range-based for loop in C++ with Examples
- How to find index of a given element in a Vector in C++
- Print all lexicographical greater permutations of a given string
- Introduction to Complex Objects and Composition
- NULL undeclared error in C/C++ and how to resolve it
- std::remove_const in C++ with Examples

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.