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:

  1. Count the total number of digits in the number N using the approach discussed in this article.
  2. Count the total number of distinct digits in N.
  3. If the count of total number of digits and 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 number
int 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 number
int 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 digits
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
int main()
{
    int n = 2019;
  
    cout << nextNumberDistinctDigit(n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find next consecutive 
// Number with all distinct digits 
class 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find next consecutive 
# Number with all distinct digits 
import sys
  
INT_MAX = sys.maxsize;
  
# Function to count distinct
# digits in a number
def 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 number
def 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 digits
def 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 code
if __name__ == '__main__':
    n = 2019;
  
    print(nextNumberDistinctDigit(n));
      
# This code is contributed by PrinciRaj1992

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find next consecutive 
// Number with all distinct digits 
using 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

chevron_right


Output:

2031



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01, princiraj1992

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.