Find an integer in the given range that satisfies the given conditions | Python3 implementation of the approach ; Function that returns true if x contains all distinct digits ; Last digit of x ; If current digit has appeared before ; Mark the current digit to present ; Remove the last digit ; Function to return the required value of k ; To store the maximum value for the given expression ; If i contains all distinct digits ; If the value of the expression is also maximum then update k and the expression ; Driver code
import sys NEW_LINE MAX = 10 NEW_LINE def distinctDigits ( x ) : NEW_LINE INDENT present = [ False for i in range ( MAX ) ] NEW_LINE while ( x > 0 ) : NEW_LINE INDENT digit = x % 10 NEW_LINE if ( present [ digit ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT present [ digit ] = True NEW_LINE x = x // 10 NEW_LINE DEDENT return True NEW_LINE DEDENT def findK ( l , r ) : NEW_LINE INDENT maxExp = - sys . maxsize - 1 NEW_LINE k = - 1 NEW_LINE for i in range ( l , r + 1 , 1 ) : NEW_LINE INDENT if ( distinctDigits ( i ) ) : NEW_LINE INDENT exp = ( l - i ) * ( i - r ) NEW_LINE if ( exp >= maxExp ) : NEW_LINE INDENT k = i ; NEW_LINE maxExp = exp NEW_LINE DEDENT DEDENT DEDENT return k NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT l = 50 NEW_LINE r = 60 NEW_LINE print ( findK ( l , r ) ) NEW_LINE DEDENT