Find an integer in the given range that satisfies the given conditions | Java 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
class GFG { static int MAX = 10 ; static boolean distinctDigits ( int x ) { boolean [ ] present = new boolean [ MAX ] ; while ( x > 0 ) { int digit = x % 10 ; if ( present [ digit ] ) return false ; present [ digit ] = true ; x /= 10 ; } return true ; } static int findK ( int l , int r ) { int maxExp = Integer . MIN_VALUE ; int k = - 1 ; for ( int i = l ; i <= r ; i ++ ) { if ( distinctDigits ( i ) ) { int exp = ( l - i ) * ( i - r ) ; if ( exp >= maxExp ) { k = i ; maxExp = exp ; } } } return k ; } public static void main ( String [ ] args ) { int l = 50 , r = 60 ; System . out . print ( findK ( l , r ) ) ; } }