Print all permutations of a number N greater than itself

Given a number N, our task is to print those permutations of integer N which are greater than N.


Input: N = 534
Output: 543

Input: N = 324
Output: 342, 423, 432

Approach: To solve this problem, we can obtain all the lexicographically larger permutations of N using next_permutation() method in C++. After getting all such numbers, print them.

For other languages, find the permutations of number N and print the numbers which are greater than N.

Below is the implementation of above approach:





// C++ implementation to print all the
// permutation greater than the integer N
#include <bits/stdc++.h>
using namespace std;
// Function to print all the permutation
// which are greater than N itself
void printPermutation(int N)
    int temp = N, count = 0;
    // Iterate and count the
    // number of digits in N
    while (temp > 0) {
        temp /= 10;
    // vector to print the
    // permutations of N
    vector<int> num(count);
    // Store digits of N
    // in the vector num
    while (N > 0) {
        num[count-- - 1] = N % 10;
        N = N / 10;
    // Iterate over every permutation of N
    // which is greater than N
    while (next_permutation(
        num.begin(), num.end())) {
        // Print the current permutaion of N
        for (int i = 0; i < num.size(); i++)
            cout << num[i];
        cout << "\n";
// Driver Code
int main()
    int N = 324;
    return 0;




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.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to 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.