Next Number with distinct digits | Python3 program to find next consecutive Number with all distinct digits ; Function to count distinct digits in a number ; To count the occurrence of digits in number from 0 to 9 ; Iterate over the digits of the number Flag those digits as found in the array ; Traverse the array arr and count the distinct digits in the array ; Function to return the total number of digits in the number ; Iterate over the digits of the number ; Function to return the next number with distinct digits ; Count the distinct digits in N + 1 ; Count the total number of digits in N + 1 ; Return the next consecutive number ; Increment Number by 1 ; Driver code
import sys NEW_LINE INT_MAX = sys . maxsize ; NEW_LINE def countDistinct ( n ) : NEW_LINE INDENT arr = [ 0 ] * 10 ; NEW_LINE count = 0 ; NEW_LINE while ( n != 0 ) : NEW_LINE INDENT r = int ( n % 10 ) ; NEW_LINE arr [ r ] = 1 ; NEW_LINE n //= 10 ; NEW_LINE DEDENT for i in range ( 10 ) : NEW_LINE INDENT if ( arr [ i ] != 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT def countDigit ( n ) : NEW_LINE INDENT c = 0 ; NEW_LINE while ( n != 0 ) : NEW_LINE INDENT r = n % 10 ; NEW_LINE c += 1 ; NEW_LINE n //= 10 ; NEW_LINE DEDENT return c ; NEW_LINE DEDENT def nextNumberDistinctDigit ( n ) : NEW_LINE INDENT while ( n < INT_MAX ) : NEW_LINE INDENT distinct_digits = countDistinct ( n + 1 ) ; NEW_LINE total_digits = countDigit ( n + 1 ) ; NEW_LINE if ( distinct_digits == total_digits ) : NEW_LINE INDENT return n + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT n += 1 ; NEW_LINE DEDENT DEDENT return - 1 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 2019 ; NEW_LINE print ( nextNumberDistinctDigit ( n ) ) ; NEW_LINE DEDENT