Given a number n, find the smallest number that has same set of digits as n and is greater than n. If n is the greatest possible number with its set of digits, then print “not possible”.
Examples:
For simplicity of implementation, we have considered input number as a string.
Input: n = "218765"
Output: "251678"
Input: n = "1234"
Output: "1243"
Input: n = "4321"
Output: "Not Possible"
Input: n = "534976"
Output: "536479"
Following are few observations about the next greater number.
- If all digits sorted in descending order, then output is always “Not Possible”. For example, 4321.
- If all digits are sorted in ascending order, then we need to swap last two digits. For example, 1234.
- For other cases, we need to process the number from rightmost side (why? because we need to find the smallest of all greater numbers)
You can now try developing an algorithm yourself.
Following is the algorithm for finding the next greater number.
- Traverse the given number from rightmost digit, keep traversing till you find a digit which is smaller than the previously traversed digit. For example, if the input number is “534976”, we stop at 4 because 4 is smaller than next digit 9. If we do not find such a digit, then output is “Not Possible”.
- Now search the right side of above found digit ‘d’ for the smallest digit greater than ‘d’. For “534976″, the right side of 4 contains “976”. The smallest digit greater than 4 is 6.
- Swap the above found two digits, we get 536974 in above example.
- Now sort all digits from position next to ‘d’ to the end of number. The number that we get after sorting is the output. For above example, we sort digits in bold 536974. We get “536479” which is the next greater number for input 534976.
Following are the implementation of above approach.
C++
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
void swap( char *a, char *b)
{
char temp = *a;
*a = *b;
*b = temp;
}
void findNext( char number[], int n)
{
int i, j;
for (i = n-1; i > 0; i--)
if (number[i] > number[i-1])
break ;
if (i==0)
{
cout << "Next number is not possible" ;
return ;
}
int x = number[i-1], smallest = i;
for (j = i+1; j < n; j++)
if (number[j] > x && number[j] < number[smallest])
smallest = j;
swap(&number[smallest], &number[i-1]);
sort(number + i, number + n);
cout << "Next number with same set of digits is " << number;
return ;
}
int main()
{
char digits[] = "534976" ;
int n = strlen (digits);
findNext(digits, n);
return 0;
}
|
Java
import java.util.Arrays;
public class nextGreater
{
static void swap( char ar[], int i, int j)
{
char temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
static void findNext( char ar[], int n)
{
int i;
for (i = n - 1 ; i > 0 ; i--)
{
if (ar[i] > ar[i - 1 ]) {
break ;
}
}
if (i == 0 )
{
System.out.println( "Not possible" );
}
else
{
int x = ar[i - 1 ], min = i;
for ( int j = i + 1 ; j < n; j++)
{
if (ar[j] > x && ar[j] < ar[min])
{
min = j;
}
}
swap(ar, i - 1 , min);
Arrays.sort(ar, i, n);
System.out.print( "Next number with same" +
" set of digits is " );
for (i = 0 ; i < n; i++)
System.out.print(ar[i]);
}
}
public static void main(String[] args)
{
char digits[] = { '5' , '3' , '4' , '9' , '7' , '6' };
int n = digits.length;
findNext(digits, n);
}
}
|
Python3
def findNext(number,n):
for i in range (n - 1 , 0 , - 1 ):
if number[i] > number[i - 1 ]:
break
if i = = 1 and number[i] < = number[i - 1 ]:
print ( "Next number not possible" )
return
x = number[i - 1 ]
smallest = i
for j in range (i + 1 ,n):
if number[j] > x and number[j] < number[smallest]:
smallest = j
number[smallest],number[i - 1 ] = number[i - 1 ], number[smallest]
x = 0
for j in range (i):
x = x * 10 + number[j]
number = sorted (number[i:])
for j in range (n - i):
x = x * 10 + number[j]
print ( "Next number with set of digits is" ,x)
digits = "534976"
number = list ( map ( int ,digits))
findNext(number, len (digits))
|
C#
using System;
public class nextGreater
{
static void swap( char []ar, int i, int j)
{
char temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
static void findNext( char []ar, int n)
{
int i;
for (i = n - 1; i > 0; i--)
{
if (ar[i] > ar[i - 1])
{
break ;
}
}
if (i == 0)
{
Console.WriteLine( "Not possible" );
}
else
{
int x = ar[i - 1], min = i;
for ( int j = i + 1; j < n; j++)
{
if (ar[j] > x && ar[j] < ar[min])
{
min = j;
}
}
swap(ar, i - 1, min);
Array.Sort(ar, i, n-i);
Console.Write( "Next number with same" +
" set of digits is " );
for (i = 0; i < n; i++)
Console.Write(ar[i]);
}
}
public static void Main(String[] args)
{
char []digits = { '5' , '3' , '4' , '9' , '7' , '6' };
int n = digits.Length;
findNext(digits, n);
}
}
|
Javascript
<script>
function findNext(number, n)
{
for ( var i = n - 1; i >= 0; i--)
{
if (number[i] > number[i - 1])
break ;
}
if (i == 1 && number[i] <= number[i - 1])
{
document.write( "Next number not possible" );
return ;
}
let x = number[i - 1];
let smallest = i;
for (let j = i + 1; j < n; j++)
{
if (number[j] > x &&
number[j] < number[smallest])
smallest = j;
}
let temp = number[smallest];
number[smallest] = number[i - 1];
number[i - 1] = temp;
x = 0
for (let j = 0; j < i; j++)
x = x * 10 + number[j];
number = number.slice(i, number.length + 1);
number.sort()
for (let j = 0; j < n - i; j++)
x = x * 10 + number[j];
document.write( "Next number with " +
"set of digits is " + x);
}
let digits = "534976"
let number = []
for (let i = 0; i < digits.length; i++)
number[i] = Number(digits[i]);
findNext(number, digits.length);
</script>
|
OutputNext number with same set of digits is 536479
Time Complexity: O(N*logN)
Auxiliary Space: O(1)
The above implementation can be optimized in following ways.
- We can use binary search in step II instead of linear search.
- In step IV, instead of doing simple sort, we can apply some clever technique to do it in linear time. Hint: We know that all digits are linearly sorted in reverse order except one digit which was swapped.
With above optimizations, we can say that the time complexity of this method is O(n).
Optimised Approach:
- Here instead of sorting the digits after (i-1) index, we are reversing the digits as mentioned in the above optimisation point.
- As they will be in decreasing order so to find the smallest element possible from the right part we just reverse them thus reducing time complexity.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
vector< int > nextPermutation( int n, vector< int > arr)
{
if (n == 1)
return arr;
int i = 0;
for (i = n - 1; i > 0; i--) {
if (arr[i] > arr[i - 1])
break ;
}
if (i != 0) {
for ( int j = n - 1; j >= i; j--) {
if (arr[i - 1] < arr[j]) {
swap(arr[i - 1], arr[j]);
break ;
}
}
}
reverse(arr.begin() + i, arr.end());
return arr;
}
int main()
{
int n = 6;
vector< int > v{ 5,3,4,9,7,6 };
vector< int > res;
res = nextPermutation(n, v);
for ( int i = 0; i < res.size(); i++) {
cout << res[i] << " " ;
}
}
|
Java
import java.util.*;
class GFG
{
static int [] nextPermutation( int n, int [] arr)
{
if (n == 1 )
return arr;
int i = 0 ;
for (i = n - 1 ; i > 0 ; i--) {
if (arr[i] > arr[i - 1 ])
break ;
}
if (i != 0 )
{
for ( int j = n - 1 ; j >= i; j--)
{
if (arr[i - 1 ] < arr[j])
{
int temp = arr[j];
arr[j] = arr[i - 1 ];
arr[i - 1 ] = temp;
break ;
}
}
}
int [] res = new int [arr.length];
int ind = arr.length - 1 ;
for ( int j = 0 ; j < i; j++)
res[j] = arr[j];
for ( int j = i; j< res.length; j++)
res[j] = arr[ind--];
return res;
}
public static void main(String[] args)
{
int n = 6 ;
int [] v = { 5 , 3 , 4 , 9 , 7 , 6 };
int [] res;
res = nextPermutation(n, v);
for ( int i = 0 ; i < res.length; i++) {
System.out.print(res[i] + " " );
}
}
}
|
Python3
def nextPermutation(arr):
n = len (arr)
k = n - 2
while k > = 0 :
if arr[k] < arr[k + 1 ]:
break
k - = 1
if k < 0 :
arr = arr[:: - 1 ]
else :
for l in range (n - 1 , k, - 1 ):
if arr[l] > arr[k]:
break
arr[l], arr[k] = arr[k], arr[l]
arr[k + 1 :] = reversed (arr[k + 1 :])
return arr
arr = [ 5 , 3 , 4 , 9 , 7 , 6 ]
print ( * nextPermutation(arr))
|
C#
using System;
class GFG {
static int [] nextPermutation( int n, int [] arr)
{
if (n == 1)
return arr;
int i = 0;
for (i = n - 1; i > 0; i--) {
if (arr[i] > arr[i - 1])
break ;
}
if (i != 0) {
for ( int j = n - 1; j >= i; j--) {
if (arr[i - 1] < arr[j]) {
int temp = arr[j];
arr[j] = arr[i - 1];
arr[i - 1] = temp;
break ;
}
}
}
int [] res = new int [arr.Length];
int ind = arr.Length - 1;
for ( int j = 0; j < i; j++)
res[j] = arr[j];
for ( int j = i; j < res.Length; j++)
res[j] = arr[ind--];
return res;
}
public static int Main()
{
int n = 6;
int [] v = new int [] { 5, 3, 4, 9, 7, 6 };
int [] res;
res = nextPermutation(n, v);
for ( int i = 0; i < res.Length; i++) {
Console.Write(res[i] + " " );
}
return 0;
}
}
|
Javascript
<script>
function nextPermutation(n, arr)
{
if (n == 1)
return arr;
let i = 0;
for (i = n - 1; i > 0; i--) {
if (arr[i] > arr[i - 1])
break ;
}
if (i != 0)
{
for (let j = n - 1; j >= i; j--)
{
if (arr[i - 1] < arr[j])
{
let temp = arr[i - 1];
arr[i - 1] = arr[j];
arr[j] = temp;
break ;
}
}
}
arr = arr.slice(0,i).concat(arr.slice(i,arr.length).reverse());
return arr;
}
let v = [5,3,4,9,7,6];
let n = 6;
let res = nextPermutation(n, v);
for (let i = 0; i < res.length; i++) {
document.write(res[i] + " " )
}
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)