Print all permutations of a number N greater than itself
Last Updated :
19 Sep, 2022
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
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++
#include <bits/stdc++.h>
using namespace std;
void printPermutation( int N)
{
int temp = N, count = 0;
while (temp > 0) {
count++;
temp /= 10;
}
vector< int > num(count);
while (N > 0) {
num[count-- - 1] = N % 10;
N = N / 10;
}
while (next_permutation(
num.begin(), num.end())) {
for ( int i = 0; i < num.size(); i++)
cout << num[i];
cout << "\n" ;
}
}
int main()
{
int N = 324;
printPermutation(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void printPermutation( int N)
{
int temp = N, count = 0 ;
while (temp > 0 )
{
count++;
temp /= 10 ;
}
int [] num = new int [count];
while (N > 0 )
{
num[count-- - 1 ] = N % 10 ;
N = N / 10 ;
}
while (next_permutation(num))
{
for ( int i = 0 ; i < num.length; i++)
System.out.print(num[i]);
System.out.print( "\n" );
}
}
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 ;
}
public static void main(String[] args)
{
int N = 324 ;
printPermutation(N);
}
}
|
Python3
def next_permutation(p):
for a in range ( len (p) - 2 , - 1 , - 1 ):
if (p[a] < p[a + 1 ]):
b = len (p) - 1
while True :
if (p[b] > p[a]):
t = p[a];
p[a] = p[b];
p[b] = t;
a + = 1
b = len (p) - 1
while a < b:
t = p[a];
p[a] = p[b];
p[b] = t;
a + = 1
b - = 1
return True ;
b - = 1
return False ;
def printPermutation(N):
temp = N
count = 0 ;
while (temp > 0 ):
count + = 1
temp = int (temp / 10 )
num = [ 0 for _ in range (count)];
while (N > 0 ):
count - = 1
num[count ] = N % 10 ;
N = int (N / 10 );
while (next_permutation(num)):
print ( * num, sep = "")
N = 324 ;
printPermutation(N);
|
C#
using System;
class GFG{
static void printPermutation( int N)
{
int temp = N, count = 0;
while (temp > 0)
{
count++;
temp /= 10;
}
int [] num = new int [count];
while (N > 0)
{
num[count-- - 1] = N % 10;
N = N / 10;
}
while (next_permutation(num))
{
for ( int i = 0; i < num.Length; i++)
Console.Write(num[i]);
Console.Write( "\n" );
}
}
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 ;
}
public static void Main(String[] args)
{
int N = 324;
printPermutation(N);
}
}
|
Javascript
<script>
function printPermutation(N)
{
let temp = N, count = 0;
while (temp > 0)
{
count++;
temp = parseInt(temp / 10, 10);
}
let num = new Array(count);
num.fill(0);
while (N > 0)
{
num[count-- - 1] = N % 10;
N = parseInt(N / 10, 10);
}
while (next_permutation(num))
{
for (let i = 0; i < num.length; i++)
document.write(num[i]);
document.write( "</br>" );
}
}
function next_permutation(p)
{
for (let a = p.length - 2; a >= 0; --a)
if (p[a] < p[a + 1])
for (let b = p.length - 1;; --b)
if (p[b] > p[a])
{
let 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 ;
}
let N = 324;
printPermutation(N);
</script>
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...