Find Next number having distinct digits from the given number N
Last Updated :
26 May, 2022
Given a natural number N, the task is to find the next number having distinct digits from the given number.
Examples:
Input: N = 19
Output: 20
Explanation:
Next number to 19 whose digits are different from 19 is 20.
Input: N = 2019
Output: 3333
Explanation:
Next number to 2019 whose digits are different from 2019 is 3333.
Approach: The idea is to use hashing to compute the next number with distinct digit from the given number –
- Create a hash array to store the digits present in the number, store the most significant digit and also store the number of digits present in the number in a count variable
- Find the next digit for the most significant bit which is not present in the number and greater than the current most significant digit.
- If the next most significant digit is not found then increase the number of digits by incrementing the count and find the most significant digit between 1 to 9 which is not present in the number.
- If the next most significant digit is found then find the minimum digit next which is not present in the number between 0 to 9.
- Iterate over the number of digits from 1 to count
- Multiply the most significant digit by 10 and add the next digit which is not present in the given number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findNextNumber( int n)
{
int h[10] = { 0 };
int i = 0, msb = n, rem = 0;
int next_num = -1, count = 0;
while (msb > 9) {
rem = msb % 10;
h[rem] = 1;
msb /= 10;
count++;
}
h[msb] = 1;
count++;
for (i = msb + 1; i < 10; i++) {
if (h[i] == 0) {
next_num = i;
break ;
}
}
if (next_num == -1){
for (i = 1; i < msb; i++){
if (h[i] == 0){
next_num = i;
count++;
break ;
}
}
}
if (next_num > 0){
for (i = 0; i < 10; i++) {
if (h[i] == 0) {
msb = i;
break ;
}
}
for (i = 1; i < count; i++) {
next_num = ((next_num * 10) + msb);
}
if (next_num > n)
cout << next_num << "\n" ;
else
cout << "Not Possible \n" ;
}
else {
cout << "Not Possible \n" ;
}
}
int main()
{
int n = 2019;
findNextNumber(n);
return 0;
}
|
C
#include <stdio.h>
void findNextNumber( int n)
{
int h[10] = { 0 };
int i = 0, msb = n, rem = 0;
int next_num = -1, count = 0;
while (msb > 9) {
rem = msb % 10;
h[rem] = 1;
msb /= 10;
count++;
}
h[msb] = 1;
count++;
for (i = msb + 1; i < 10; i++) {
if (h[i] == 0) {
next_num = i;
break ;
}
}
if (next_num == -1){
for (i = 1; i < msb; i++){
if (h[i] == 0){
next_num = i;
count++;
break ;
}
}
}
if (next_num > 0){
for (i = 0; i < 10; i++) {
if (h[i] == 0) {
msb = i;
break ;
}
}
for (i = 1; i < count; i++) {
next_num = ((next_num * 10) + msb);
}
if (next_num > n)
printf ( "%d\n" , next_num);
else
printf ( "\nNot Possible" );
}
else {
printf ( "Not Possible \n" );
}
}
int main()
{
int n = 2019;
findNextNumber(n);
return 0;
}
|
Java
class GFG{
static void findNextNumber( int n)
{
int h[] = new int [ 10 ];
int i = 0 , msb = n, rem = 0 ;
int next_num = - 1 , count = 0 ;
while (msb > 9 ) {
rem = msb % 10 ;
h[rem] = 1 ;
msb /= 10 ;
count++;
}
h[msb] = 1 ;
count++;
for (i = msb + 1 ; i < 10 ; i++) {
if (h[i] == 0 ) {
next_num = i;
break ;
}
}
if (next_num == - 1 ){
for (i = 1 ; i < msb; i++){
if (h[i] == 0 ){
next_num = i;
count++;
break ;
}
}
}
if (next_num > 0 ){
for (i = 0 ; i < 10 ; i++) {
if (h[i] == 0 ) {
msb = i;
break ;
}
}
for (i = 1 ; i < count; i++) {
next_num = ((next_num * 10 ) + msb);
}
if (next_num > n)
System.out.print(next_num+ "\n" );
else
System.out.print( "Not Possible \n" );
}
else {
System.out.print( "Not Possible \n" );
}
}
public static void main(String[] args)
{
int n = 2019 ;
findNextNumber(n);
}
}
|
C#
using System;
class GFG{
static void findNextNumber( int n)
{
int []h = new int [10];
int i = 0, msb = n, rem = 0;
int next_num = -1, count = 0;
while (msb > 9) {
rem = msb % 10;
h[rem] = 1;
msb /= 10;
count++;
}
h[msb] = 1;
count++;
for (i = msb + 1; i < 10; i++) {
if (h[i] == 0) {
next_num = i;
break ;
}
}
if (next_num == -1){
for (i = 1; i < msb; i++){
if (h[i] == 0){
next_num = i;
count++;
break ;
}
}
}
if (next_num > 0){
for (i = 0; i < 10; i++) {
if (h[i] == 0) {
msb = i;
break ;
}
}
for (i = 1; i < count; i++) {
next_num = ((next_num * 10) + msb);
}
if (next_num > n)
Console.WriteLine(next_num);
else
Console.WriteLine( "Not Possible" );
}
else {
Console.WriteLine( "Not Possible" );
}
}
public static void Main( string [] args)
{
int n = 2019;
findNextNumber(n);
}
}
|
Python 3
def findNextNumber(n):
h = [ 0 for i in range ( 10 )]
i = 0
msb = n
rem = 0
next_num = - 1
count = 0
while (msb > 9 ):
rem = msb % 10
h[rem] = 1
msb / / = 10
count + = 1
h[msb] = 1
count + = 1
for i in range (msb + 1 , 10 , 1 ):
if (h[i] = = 0 ):
next_num = i
break
if (next_num = = - 1 ):
for i in range ( 1 ,msb, 1 ):
if (h[i] = = 0 ):
next_num = i
count + = 1
break
if (next_num > 0 ):
for i in range ( 0 , 10 , 1 ):
if (h[i] = = 0 ):
msb = i
break
for i in range ( 1 ,count, 1 ):
next_num = ((next_num * 10 ) + msb)
if (next_num > n):
print (next_num)
else :
print ( "Not Possible" )
else :
print ( "Not Possible" )
if __name__ = = '__main__' :
n = 2019
findNextNumber(n)
|
Javascript
<script>
function findNextNumber(n)
{
let h = Array.from({length: 10}, (_, i) => 0);
let i = 0, msb = n, rem = 0;
let next_num = -1, count = 0;
while (msb > 9) {
rem = msb % 10;
h[rem] = 1;
msb = Math.floor(msb / 10);
count++;
}
h[msb] = 1;
count++;
for (i = msb + 1; i < 10; i++) {
if (h[i] == 0) {
next_num = i;
break ;
}
}
if (next_num == -1){
for (i = 1; i < msb; i++){
if (h[i] == 0){
next_num = i;
count++;
break ;
}
}
}
if (next_num > 0){
for (i = 0; i < 10; i++) {
if (h[i] == 0) {
msb = i;
break ;
}
}
for (i = 1; i < count; i++) {
next_num = ((next_num * 10) + msb);
}
if (next_num > n)
document.write(next_num+ "\n" );
else
document.write( "Not Possible \n" );
}
else {
document.write( "Not Possible \n" );
}
}
let n = 2019;
findNextNumber(n);
</script>
|
Time Complexity: O(logN)
Auxiliary Space: O(logN)
Share your thoughts in the comments
Please Login to comment...