# Print all permutations of a number N greater than itself

• Difficulty Level : Hard
• Last Updated : 18 Aug, 2021

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

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

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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++

 `// C++ implementation to print all the``// permutation greater than the integer N` `#include ``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) {``        ``count++;``        ``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 permutation of N``        ``for` `(``int` `i = 0; i < num.size(); i++)``            ``cout << num[i];` `        ``cout << ``"\n"``;``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `N = 324;` `    ``printPermutation(N);` `    ``return` `0;``}`

## Java

 `// Java implementation to print all the``// permutation greater than the integer N``import` `java.util.*;``class` `GFG{` `static` `void` `printPermutation(``int` `N)``{``    ``int` `temp = N, count = ``0``;` `    ``// Iterate and count the``    ``// number of digits in N``    ``while` `(temp > ``0``)``    ``{``      ``count++;``      ``temp /= ``10``;``    ``}` `    ``// vector to print the``    ``// permutations of N``    ``int``[] num = ``new` `int``[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))``    ``{` `      ``// Print the current permutation of N``      ``for` `(``int` `i = ``0``; i < num.length; i++)``        ``System.out.print(num[i]);` `      ``System.out.print(``"\n"``);``    ``}``  ``}` `  ``// Function to print all the permutation``  ``// which are greater than N itself``  ``static` `boolean` `next_permutation(``int``[] p)``  ``{``    ``for` `(``int` `a = p.length - ``2``; a >= ``0``; --a)``      ``if` `(p[a] < p[a + ``1``])``        ``for` `(``int` `b = p.length - ``1``;; --b)``          ``if` `(p[b] > p[a])``          ``{``            ``int` `t = p[a];``            ``p[a] = p[b];``            ``p[b] = t;``            ``for` `(++a, b = p.length - ``1``; a < b; ++a, --b)``            ``{``              ``t = p[a];``              ``p[a] = p[b];``              ``p[b] = t;``            ``}``            ``return` `true``;``          ``}``    ``return` `false``;``  ``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``  ``int` `N = ``324``;` `  ``printPermutation(N);``}``}` `// This code contributed by sapnasingh4991`

## C#

 `// C# implementation to print all the``// permutation greater than the integer N``using` `System;``class` `GFG{` `static` `void` `printPermutation(``int` `N)``{``    ``int` `temp = N, count = 0;` `    ``// Iterate and count the``    ``// number of digits in N``    ``while` `(temp > 0)``    ``{``      ``count++;``      ``temp /= 10;``    ``}` `    ``// vector to print the``    ``// permutations of N``    ``int``[] num = ``new` `int``[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))``    ``{` `      ``// Print the current permutation of N``      ``for` `(``int` `i = 0; i < num.Length; i++)``        ``Console.Write(num[i]);` `      ``Console.Write(``"\n"``);``    ``}``  ``}` `  ``// Function to print all the permutation``  ``// which are greater than N itself``  ``static` `bool` `next_permutation(``int``[] p)``  ``{``    ``for` `(``int` `a = p.Length - 2; a >= 0; --a)``      ``if` `(p[a] < p[a + 1])``        ``for` `(``int` `b = p.Length - 1;; --b)``          ``if` `(p[b] > p[a])``          ``{``            ``int` `t = p[a];``            ``p[a] = p[b];``            ``p[b] = t;``            ``for` `(++a, b = p.Length - 1;``                       ``a < b; ++a, --b)``            ``{``              ``t = p[a];``              ``p[a] = p[b];``              ``p[b] = t;``            ``}``            ``return` `true``;``          ``}``    ``return` `false``;``  ``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``  ``int` `N = 324;` `  ``printPermutation(N);``}``}` `// This code is contributed by Rohit_ranjan`

## Javascript

 ``
Output:
```342
423
432```

My Personal Notes arrow_drop_up