Reduce a number N by at most D to maximize count of trailing nines | Function to find a number with maximum count of trailing nine ; Stores count of digits in n ; Stores power of 10 ; If last i digits greater than or equal to d ; Update res ; Update p10 ; Driver Code ; Function Call
function maxNumTrailNine ( n , d ) { let res = n ; let cntDigits = parseInt ( Math . log ( n ) / Math . log ( 10 ) ) + 1 ; let p10 = 10 ; for ( let i = 1 ; i <= cntDigits ; i ++ ) { if ( n % p10 >= d ) { break ; } else { res = n - n % p10 - 1 ; } p10 = p10 * 10 ; } document . write ( res ) ; } let n = 1025 , d = 6 ; maxNumTrailNine ( n , d ) ;