All Reverse Permutations of an Array Using STL in C++



In this problem, we need to generate all reverse permutations of a given array using C++'s Standard Template Library (STL). A permutation is simply rearranging the elements of the array in every possible order. For example, with an array of three elements, there are six possible ways to arrange them.

For each of these permutations, we need to reverse the order of the elements and then print the results. The goal is to first generate all possible permutations of the array, then reverse each one before displaying them.

Example Scenario:

Let's say we have the array {1, 2, 3}. The forward permutations would be all the different ways we can arrange the numbers, such as:

1, 2, 3
1, 3, 2
2, 1, 3
2, 3, 1
3, 1, 2
3, 2, 1

After reversing each of these permutations, the reversed permutations become:

3, 2, 1
3, 1, 2
2, 3, 1
2, 1, 3
1, 3, 2
1, 2, 3

In this article, we will show you how to use C++ STL to generate all reverse permutations of an array, step by step.

Approaches for Generating Reverse Permutations

We can use different approaches to generate reverse permutations. Some of the common approaches are:

Using next_permutation and reverse

In this approach, we generate all the permutations of an array using the next_permutation function in C++. After generating each permutation, we reverse it using the reverse function to get the reversed permutation. The next_permutation function helps us go through all possible arrangements, and reverse simply flips the order of each one

Example

In this example, we take an array, sort it to ensure the permutations start correctly, and then use a loop to generate each permutation. For each permutation, we create a copy of the array, reverse it, and then print the reversed array.

#include <iostream>
#include <algorithm>
#include <vector>

int main() {
    std::vector<int> arr = {1, 2, 3};
    // Sort to ensure the permutations start correctly
    std::sort(arr.begin(), arr.end());

    do {
        // Copy the array
        std::vector<int> reversed_arr = arr;
        // Reverse the copied array
        std::reverse(reversed_arr.begin(), reversed_arr.end());
        for (int num : reversed_arr) {
            std::cout << num << " ";
        }
        std::cout << std::endl;
        // Generate next permutation
    } while (std::next_permutation(arr.begin(), arr.end()));

    return 0;
}

In this output, each line represents a reversed permutation of the original array.

3 2 1 
3 1 2 
2 3 1 
2 1 3 
1 3 2 
1 2 3

Time Complexity: O(n! * n), where n is the number of elements in the array. We generate n! permutations, and for each, we reverse it, which takes O(n).

Space Complexity: O(n), as we are using an additional array to store the reversed permutations.

Using prev_permutation

In this approach, we use the prev_permutation function to generate the previous permutation in lexicographical order. By using prev_permutation, we start from the largest permutation and work backwards. After generating each permutation, we reverse the order of its elements to get the reverse permutation.

Example

In this example, we start with the array {1, 2, 3}, sort it in descending order to get {3, 2, 1}, and use prev_permutation to generate all the previous permutations in reverse lexicographical order. We then print each permutation to obtain all the reverse permutations of the array.

#include <iostream>
#include <algorithm>
#include <vector>

int main() {
    std::vector<int> arr = {1, 2, 3};
    std::sort(arr.begin(), arr.end(), std::greater<int>());  // Sort in descending order to start from the largest permutation

    do {
        // Print the current permutation directly
        for (int num : arr) {
            std::cout << num << " ";  // Print the permutation
        }
        std::cout << std::endl;
    } while (std::prev_permutation(arr.begin(), arr.end()));  // Generate previous permutation

    return 0;
}

The output below prints the reverse of each permutation of the array {1, 2, 3}.

3 2 1 
3 1 2 
2 3 1 
2 1 3 
1 3 2 
1 2 3

Time Complexity: O(n * n!) because there are n! permutations, and each prev_permutation call takes O(n) time.

Space Complexity: O(n) as the algorithm uses only the input array and a few extra variables.

Using Recursion

In this approach, we use a recursive algorithm to generate all permutations of an array and reverse each one. This method gives you full control over the process, allowing customization without relying on STL's next_permutation or prev_permutation.

Example

In this example, we use a backtracking technique to generate all permutations of the array {1, 2, 3}. After each permutation is generated, we reverse it and store it in the result vector. This way, the result will contain all the reverse permutations of the original array.

#include <iostream>
#include <vector>
#include <algorithm>

void generatePermutations(std::vector<int>& arr, int start, std::vector<std::vector<int>>& result) {
    if (start == arr.size()) {
        std::vector<int> reversed_arr = arr;
        std::reverse(reversed_arr.begin(), reversed_arr.end());  // Reverse the permutation
        result.push_back(reversed_arr);
        return;
    }

    for (int i = start; i < arr.size(); i++) {
        std::swap(arr[start], arr[i]);
        generatePermutations(arr, start + 1, result);
        std::swap(arr[start], arr[i]);  // backtrack
    }
}

int main() {
    std::vector<int> arr = {1, 2, 3};
    std::vector<std::vector<int>> result;

    generatePermutations(arr, 0, result);

    for (const auto& perm : result) {
        for (int num : perm) {
            std::cout << num << " ";
        }
        std::cout << std::endl;
    }

    return 0;
}

The program generates all permutations of the array {1, 2, 3}, reverses each permutation, and prints them.

3 2 1 
2 3 1 
3 1 2 
1 3 2 
1 2 3 
2 1 3

Time Complexity: O(n!), where n is the number of elements in the array. We generate all n! permutations recursively.

Space Complexity: O(n), due to the space needed for the recursion stack.

Conclusion

In this article, we looked at three ways to generate reverse permutations in C++: using next_permutation with reverse, prev_permutation, and backtracking. Each method helps us easily generate and reverse the permutations of an array. These approaches give us different ways to work with permutations in C++.

Updated on: 2025-02-21T16:30:01+05:30

265 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements