Reduce a number N by at most D to maximize count of trailing nines | CPP 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
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; void maxNumTrailNine ( int n , int d ) { int res = n ; int cntDigits = 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 ; } cout << res ; } int main ( ) { int n = 1025 , d = 6 ; maxNumTrailNine ( n , d ) ; }