Reduce a number N by at most D to maximize count of trailing nines | Java program for the above approach ; 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
class GFG { static void maxNumTrailNine ( int n , int d ) { int res = n ; int cntDigits = ( int ) Math . log10 ( n ) + 1 ; int p10 = 10 ; for ( int i = 1 ; i <= cntDigits ; i ++ ) { if ( n % p10 >= d ) { break ; } else { res = n - n % p10 - 1 ; } p10 = p10 * 10 ; } System . out . println ( res ) ; } public static void main ( String [ ] args ) { int n = 1025 , d = 6 ; maxNumTrailNine ( n , d ) ; } }