Next Number with distinct digits - GeeksforGeeks

06 Apr, 2022
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.
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++
Java
Python3
C#
Javascript
// C++ program to find next consecutive// Number with all distinct digits#include <bits/stdc++.h>using namespace std; // Function to count distinct// digits in a numberint countDistinct(int n){    // To count the occurrence of digits    // in number from 0 to 9    int arr[10] = { 0 };    int count = 0;     // Iterate over the digits of the number    // Flag those digits as found in the array    while (n) {        int r = n % 10;        arr[r] = 1;        n /= 10;    }     // Traverse the array arr and count the    // distinct digits in the array    for (int i = 0; i < 10; i++) {        if (arr[i])            count++;    }    return count;} // Function to return the total number// of digits in the numberint countDigit(int n){    int c = 0;     // Iterate over the digits of the number    while (n) {        int r = n % 10;        c++;        n /= 10;    }    return c;} // Function to return the next// number with distinct digitsint nextNumberDistinctDigit(int n){    while (n < INT_MAX) {         // Count the distinct digits in N + 1        int distinct_digits = countDistinct(n + 1);         // Count the total number of digits in N + 1        int total_digits = countDigit(n + 1);         if (distinct_digits == total_digits) {             // Return the next consecutive number            return n + 1;        }         else            // Increment Number by 1            n++;    }    return -1;} // Driver codeint main(){    int n = 2019;     cout << nextNumberDistinctDigit(n);     return 0;}
// Java program to find next consecutive// Number with all distinct digitsclass GFG{         final static int INT_MAX = Integer.MAX_VALUE ;         // Function to count distinct    // digits in a number    static int countDistinct(int n)    {                 // To count the occurrence of digits        // in number from 0 to 9        int arr[] = new int[10];        int count = 0;             // Iterate over the digits of the number        // Flag those digits as found in the array        while (n != 0)        {            int r = n % 10;            arr[r] = 1;            n /= 10;        }             // Traverse the array arr and count the        // distinct digits in the array        for (int i = 0; i < 10; i++)        {            if (arr[i] != 0)                count++;        }        return count;    }         // Function to return the total number    // of digits in the number    static int countDigit(int n)    {        int c = 0;             // Iterate over the digits of the number        while (n != 0)        {            int r = n % 10;            c++;            n /= 10;        }        return c;    }         // Function to return the next    // number with distinct digits    static int nextNumberDistinctDigit(int n)    {        while (n < INT_MAX)        {                 // Count the distinct digits in N + 1            int distinct_digits = countDistinct(n + 1);                 // Count the total number of digits in N + 1            int total_digits = countDigit(n + 1);                 if (distinct_digits == total_digits)            {                     // Return the next consecutive number                return n + 1;            }                 else                             // Increment Number by 1                n++;        }        return -1;    }         // Driver code    public static void main (String[] args)    {        int n = 2019;             System.out.println(nextNumberDistinctDigit(n));    }} // This code is contributed by AnkitRai01
# Python3 program to find next consecutive# Number with all distinct digitsimport sys INT_MAX = sys.maxsize; # Function to count distinct# digits in a numberdef countDistinct(n):     # To count the occurrence of digits    # in number from 0 to 9    arr = [0] * 10;    count = 0;     # Iterate over the digits of the number    # Flag those digits as found in the array    while (n != 0):        r = int(n % 10);        arr[r] = 1;        n //= 10;         # Traverse the array arr and count the    # distinct digits in the array    for i in range(10):        if (arr[i] != 0):            count += 1;         return count; # Function to return the total number# of digits in the numberdef countDigit(n):    c = 0;     # Iterate over the digits of the number    while (n != 0):        r = n % 10;        c+=1;        n //= 10;         return c; # Function to return the next# number with distinct digitsdef nextNumberDistinctDigit(n):    while (n < INT_MAX):         # Count the distinct digits in N + 1        distinct_digits = countDistinct(n + 1);         # Count the total number of digits in N + 1        total_digits = countDigit(n + 1);         if (distinct_digits == total_digits):             # Return the next consecutive number            return n + 1;        else:             # Increment Number by 1            n += 1;         return -1; # Driver codeif __name__ == '__main__':    n = 2019;     print(nextNumberDistinctDigit(n));     # This code is contributed by PrinciRaj1992
// C# program to find next consecutive// Number with all distinct digitsusing System; class GFG{         readonly static int INT_MAX = int.MaxValue ;         // Function to count distinct    // digits in a number    static int countDistinct(int n)    {                 // To count the occurrence of digits        // in number from 0 to 9        int []arr = new int[10];        int count = 0;             // Iterate over the digits of the number        // Flag those digits as found in the array        while (n != 0)        {            int r = n % 10;            arr[r] = 1;            n /= 10;        }             // Traverse the array arr and count the        // distinct digits in the array        for (int i = 0; i < 10; i++)        {            if (arr[i] != 0)                count++;        }        return count;    }         // Function to return the total number    // of digits in the number    static int countDigit(int n)    {        int c = 0;             // Iterate over the digits of the number        while (n != 0)        {            int r = n % 10;            c++;            n /= 10;        }        return c;    }         // Function to return the next    // number with distinct digits    static int nextNumberDistinctDigit(int n)    {        while (n < INT_MAX)        {                 // Count the distinct digits in N + 1            int distinct_digits = countDistinct(n + 1);                 // Count the total number of digits in N + 1            int total_digits = countDigit(n + 1);                 if (distinct_digits == total_digits)            {                     // Return the next consecutive number                return n + 1;            }                 else                             // Increment Number by 1                n++;        }        return -1;    }         // Driver code    public static void Main(String[] args)    {        int n = 2019;             Console.WriteLine(nextNumberDistinctDigit(n));    }} // This code is contributed by PrinciRaj1992
<script>    // Javascript program to find next consecutive    // Number with all distinct digits         let INT_MAX = Number.MAX_VALUE;          // Function to count distinct    // digits in a number    function countDistinct(n)    {                  // To count the occurrence of digits        // in number from 0 to 9        let arr = new Array(10);        arr.fill(0);        let count = 0;              // Iterate over the digits of the number        // Flag those digits as found in the array        while (n != 0)        {            let r = n % 10;            arr[r] = 1;            n = parseInt(n / 10, 10);        }              // Traverse the array arr and count the        // distinct digits in the array        for (let i = 0; i < 10; i++)        {            if (arr[i] != 0)                count++;        }        return count;    }          // Function to return the total number    // of digits in the number    function countDigit(n)    {        let c = 0;              // Iterate over the digits of the number        while (n != 0)        {            let r = n % 10;            c++;            n = parseInt(n / 10, 10);        }        return c;    }          // Function to return the next    // number with distinct digits    function nextNumberDistinctDigit(n)    {        while (n < INT_MAX)        {                  // Count the distinct digits in N + 1            let distinct_digits = countDistinct(n + 1);                  // Count the total number of digits in N + 1            let total_digits = countDigit(n + 1);                  if (distinct_digits == total_digits)            {                      // Return the next consecutive number                return n + 1;            }                  else                              // Increment Number by 1                n++;        }        return -1;    }         let n = 2019;            document.write(nextNumberDistinctDigit(n));         </script>
2031
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++
Python3
Javascript
// CPP program for the above program#include <bits/stdc++.h>using namespace std; // Function to find next number// with digit distinctvoid nextNumberDistinctDigit(int n){         // Iterate from n + 1 to inf    for (int i = n + 1;; i++) {               // Convert the no. to        // string        string s = to_string(i);               // Convert string to set using stl        set<int> uniDigits(s.begin(), s.end());               // Output if condition satisfies        if (s.size() == uniDigits.size()) {            cout << i;            break;        }    }} // Driver Codeint main(){    int n = 2019; // input the no.         // Function Call    nextNumberDistinctDigit(n);    return 0;}
# Python program for the above program # Function to find next number# with digit distinctimport sys def nextNumberDistinctDigit(n):         # Iterate from n + 1 to inf    for i in range(n + 1,sys.maxsize):             # Convert the no. to        # string        s = str(i)             # Convert string to set using stl        uniDigits = set([char for char in s])             # Output if condition satisfies        if (len(s) == len(uniDigits)):            print(i)            break # Driver Coden = 2019 # input the no.     # Function CallnextNumberDistinctDigit(n) # This code is contributed by shinjanpatra
<script> // JavaScript program for the above program // Function to find next number// with digit distinctfunction nextNumberDistinctDigit(n){         // Iterate from n + 1 to inf    for (let i = n + 1;; i++) {             // Convert the no. to        // string        let s = i.toString();             // Convert string to set using stl        let uniDigits = new Set(s.split(''));             // Output if condition satisfies        if (s.length == uniDigits.size) {            document.write(i);            break;        }    }} // Driver Code let n = 2019; // input the no.     // Function CallnextNumberDistinctDigit(n); // This code is contributed by shinjanpatra </script>
2031
ankthon
princiraj1992
svrrrsvr
decode2207
shinjanpatra
number-digits
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Print all possible combinations of r elements in a given array of size n
Operators in C / C++
The Knight's tour problem | Backtracking-1
Program for factorial of a number
Program for Decimal to Binary Conversion
Find minimum number of coins that make a given value
Program to find sum of elements in a given array