Next Number with distinct digits
Given an integer N, the task is to find the next number with distinct digits in it.
Examples:
Input: N = 20
Output: 21
The next integer with all distinct digits after 20 is 21.
Input: N = 2019
Output: 2031
Approach:
- Count the total number of digits in the number N using the approach discussed in this article.
- Count the total number of distinct digits in N.
- If the count of a total number of digits and the number of distinct digits in N is equal, then return the number, otherwise, increment the number by one and repeat the previous steps.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countDistinct( int n)
{
int arr[10] = { 0 };
int count = 0;
while (n) {
int r = n % 10;
arr[r] = 1;
n /= 10;
}
for ( int i = 0; i < 10; i++) {
if (arr[i])
count++;
}
return count;
}
int countDigit( int n)
{
int c = 0;
while (n) {
int r = n % 10;
c++;
n /= 10;
}
return c;
}
int nextNumberDistinctDigit( int n)
{
while (n < INT_MAX) {
int distinct_digits = countDistinct(n + 1);
int total_digits = countDigit(n + 1);
if (distinct_digits == total_digits) {
return n + 1;
}
else
n++;
}
return -1;
}
int main()
{
int n = 2019;
cout << nextNumberDistinctDigit(n);
return 0;
}
|
Java
import java.io.*;
public class GFG
{
final static int INT_MAX = Integer.MAX_VALUE ;
static int countDistinct( int n)
{
int arr[] = new int [ 10 ];
int count = 0 ;
while (n != 0 )
{
int r = n % 10 ;
arr[r] = 1 ;
n /= 10 ;
}
for ( int i = 0 ; i < 10 ; i++)
{
if (arr[i] != 0 )
count++;
}
return count;
}
static int countDigit( int n)
{
int c = 0 ;
while (n != 0 )
{
int r = n % 10 ;
c++;
n /= 10 ;
}
return c;
}
static int nextNumberDistinctDigit( int n)
{
while (n < INT_MAX)
{
int distinct_digits = countDistinct(n + 1 );
int total_digits = countDigit(n + 1 );
if (distinct_digits == total_digits)
{
return n + 1 ;
}
else
n++;
}
return - 1 ;
}
public static void main (String[] args)
{
int n = 2019 ;
System.out.println(nextNumberDistinctDigit(n));
}
}
|
Python3
import sys
INT_MAX = sys.maxsize;
def countDistinct(n):
arr = [ 0 ] * 10 ;
count = 0 ;
while (n ! = 0 ):
r = int (n % 10 );
arr[r] = 1 ;
n / / = 10 ;
for i in range ( 10 ):
if (arr[i] ! = 0 ):
count + = 1 ;
return count;
def countDigit(n):
c = 0 ;
while (n ! = 0 ):
r = n % 10 ;
c + = 1 ;
n / / = 10 ;
return c;
def nextNumberDistinctDigit(n):
while (n < INT_MAX):
distinct_digits = countDistinct(n + 1 );
total_digits = countDigit(n + 1 );
if (distinct_digits = = total_digits):
return n + 1 ;
else :
n + = 1 ;
return - 1 ;
if __name__ = = '__main__' :
n = 2019 ;
print (nextNumberDistinctDigit(n));
|
C#
using System;
class GFG
{
readonly static int INT_MAX = int .MaxValue ;
static int countDistinct( int n)
{
int []arr = new int [10];
int count = 0;
while (n != 0)
{
int r = n % 10;
arr[r] = 1;
n /= 10;
}
for ( int i = 0; i < 10; i++)
{
if (arr[i] != 0)
count++;
}
return count;
}
static int countDigit( int n)
{
int c = 0;
while (n != 0)
{
int r = n % 10;
c++;
n /= 10;
}
return c;
}
static int nextNumberDistinctDigit( int n)
{
while (n < INT_MAX)
{
int distinct_digits = countDistinct(n + 1);
int total_digits = countDigit(n + 1);
if (distinct_digits == total_digits)
{
return n + 1;
}
else
n++;
}
return -1;
}
public static void Main(String[] args)
{
int n = 2019;
Console.WriteLine(nextNumberDistinctDigit(n));
}
}
|
Javascript
<script>
let INT_MAX = Number.MAX_VALUE;
function countDistinct(n)
{
let arr = new Array(10);
arr.fill(0);
let count = 0;
while (n != 0)
{
let r = n % 10;
arr[r] = 1;
n = parseInt(n / 10, 10);
}
for (let i = 0; i < 10; i++)
{
if (arr[i] != 0)
count++;
}
return count;
}
function countDigit(n)
{
let c = 0;
while (n != 0)
{
let r = n % 10;
c++;
n = parseInt(n / 10, 10);
}
return c;
}
function nextNumberDistinctDigit(n)
{
while (n < INT_MAX)
{
let distinct_digits = countDistinct(n + 1);
let total_digits = countDigit(n + 1);
if (distinct_digits == total_digits)
{
return n + 1;
}
else
n++;
}
return -1;
}
let n = 2019;
document.write(nextNumberDistinctDigit(n));
</script>
|
Time Complexity: O(logn)
Auxiliary Space: O(1)
Another Approach:
Instead of calculating the number of digits each time, we can use set STL in order to check if a number has only unique digits.
Then we can compare the size of string s formed from a given number and the newly created set.
For example, let us consider the number 1987, then we can convert the number into a string,
C++
int n;
cin>>n;
string s = to_string(n);
|
After that, initialize a set with the contents of string s.
C++
set< int > uniDigits(s.begin(), s.end());
|
Then we can compare the size of string s and the newly created set uniDigits.
Here is the total code
C++
#include <bits/stdc++.h>
using namespace std;
void nextNumberDistinctDigit( int n)
{
for ( int i = n + 1;; i++) {
string s = to_string(i);
set< int > uniDigits(s.begin(), s.end());
if (s.size() == uniDigits.size()) {
cout << i;
break ;
}
}
}
int main()
{
int n = 2019;
nextNumberDistinctDigit(n);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
int n = 2019 ;
nextNumberDistinctDigit(n);
}
public static void nextNumberDistinctDigit( int n) {
for ( int i = n + 1 ;; i++) {
String s = Integer.toString(i);
Set<Integer> uniDigits = new HashSet<>();
for ( int j = 0 ; j < s.length(); j++) {
uniDigits.add(Character.getNumericValue(s.charAt(j)));
}
if (s.length() == uniDigits.size()) {
System.out.println(i);
break ;
}
}
}
}
|
Python3
import sys
def nextNumberDistinctDigit(n):
for i in range (n + 1 ,sys.maxsize):
s = str (i)
uniDigits = set ([char for char in s])
if ( len (s) = = len (uniDigits)):
print (i)
break
n = 2019
nextNumberDistinctDigit(n)
|
C#
using System;
using System.Collections.Generic;
namespace NextNumberDistinctDigit
{
class Program
{
static void Main( string [] args)
{
int n = 2019;
NextNumberDistinctDigit(n);
}
public static void NextNumberDistinctDigit( int n)
{
for ( int i = n + 1;; i++)
{
string s = i.ToString();
HashSet< int > uniDigits = new HashSet< int >();
for ( int j = 0; j < s.Length; j++)
{
uniDigits.Add( int .Parse(s[j].ToString()));
}
if (s.Length == uniDigits.Count)
{
Console.WriteLine(i);
break ;
}
}
}
}
}
|
Javascript
<script>
function nextNumberDistinctDigit(n)
{
for (let i = n + 1;; i++) {
let s = i.toString();
let uniDigits = new Set(s.split( '' ));
if (s.length == uniDigits.size) {
document.write(i);
break ;
}
}
}
let n = 2019;
nextNumberDistinctDigit(n);
</script>
|
Last Updated :
20 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...