Find an integer in the given range that satisfies the given conditions | javascript 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
var MAX = 10 ; function distinctDigits ( x ) { var present = new Array ( MAX ) . fill ( false ) ; while ( x > 0 ) { var digit = x % 10 ; if ( present [ digit ] ) return false ; present [ digit ] = true ; x = parseInt ( x / 10 ) ; } return true ; } function findK ( l , r ) { var maxExp = Number . MIN_VALUE ; var k = - 1 ; for ( var i = l ; i <= r ; i ++ ) { if ( distinctDigits ( i ) ) { var exp = ( l - i ) * ( i - r ) ; if ( exp >= maxExp ) { k = i ; maxExp = exp ; } } } return k ; } var l = 50 , r = 60 ; document . write ( findK ( l , r ) ) ;