Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Implement Prev Permutation in C++ STL



The Prev permutation is an algorithmic operation that rearranges the elements of an array or a range of an array into the previous lexicographically smaller permutation. In this article, we will learn how to use the prev_permutation() function from the Standard Template Library (STL) in C++.

What is Prev Permutation?

The Prev Permutation is an operation used to generate all the possible permutations of an array in reverse lexicographical order. A permutation is one of the N! arrangements of the elements in an array of size N. The STL library of C++ provide a pre-defined function for performing the Prev Permutation operation. This operation is useful for generating reverse-ordered combinations or solving problems related to permutations in reverse order.

For example, in the code we have shown the previous lexicographical permutation of an integer array:

// Declare an Array
vector<int> nums = {2, 1, 3};

// The Prev Permutation of the array will be:
{ 1, 3, 2 }

Using prev_permutation Function in STL

The prev_permutation() function is defined in the <algorithm> header of STL. It rearranges the range [first, last) into the previous lexicographically smaller permutation. If such permutation is not possible, it rearranges the sequence into the largest possible order (sorted in descending order). Below are some points about this function:

  • Header: <algorithm>
  • Syntax:
    bool prev_permutation(Array Begin Iterator, Array End Iterator);
    
  • Return: Returns true if the function could rearrange the object as a lexicographically smaller permutation. Otherwise, returns false.

Steps to Implement Prev Permutation in C++ STL

Following are steps/algorithm to use previous permutation using C++ STL:

  • Include the <algorithm> header file.
  • Initialize a container like vector or array with the desired elements.
  • Call the sort() function in descending order to get the starting permutation.
  • Use prev_permutation() in a loop to generate and print each permutation in reverse order.

C++ Program to Implement Prev Permutation using STL

The below code is the implementation of the above algorithm in C++ language.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    vector<int> nums = {2, 1, 3};
    cout << "Does previous permutation possible?: ";
    cout << prev_permutation(nums.begin(), nums.end()) << endl;
    cout << "Previous Permutation of the Input Array: ";
    for (int num : nums) {
        cout << num << " ";
    }
    cout << endl;

    // Sort descending to get last permutation first
    sort(nums.begin(), nums.end(), greater<int>());

    cout << "All reverse permutations:" << endl;
    do {
        for (int num : nums) {
            cout << num << " ";
        }
        cout << endl;
    } while (prev_permutation(nums.begin(), nums.end()));

    return 0;
}

The output of above code will be

Does previous permutation possible?: 1
Previous Permutation of the Input Array: 1 3 2 
All reverse permutations:
3 2 1 
3 1 2 
2 3 1 
2 1 3 
1 3 2 
1 2 3 

Time and Space Complexity

Time Complexity:

  • prev_permutation(): O(n), where n is the number of elements in the container.
  • Generating all permutations: O(n × n!) for n elements.

Space Complexity: O(1) extra space as prev_permutation works in-place.

Updated on: 2025-05-09T15:39:04+05:30

119 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements