Reduce a number N by at most D to maximize count of trailing nines
Last Updated :
28 Apr, 2021
Given two positive integers N and D, the task is to decrement the value of N by at most D such that N contains the maximum count of trailing 9s.
Examples:
Input: N = 1025, D = 6
Output: 1019
Explanation:
Decrementing N by 6 modifies N to 1019, which consists of maximum possible count of trailing 9s.
Therefore, the required output is 1019.
Input: N = 1025, D = 5
Output: 1025
Decrementing N by all possible values up to D(= 5), no number can be obtained which contains trailing 9.
Therefore, the required output is 1025.
Naive Approach: The simplest approach to solve this problem is to iterate over the range [0, D] using variable i and decrement the value of N by i. Finally, print the value of N which contains the maximum possible count of trailing 9.
Time Complexity: O(D * log10(N))
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the following observations:
N % pow(10, i) gives the last i digits of N
Follow the steps below to solve the problem:
- Initialize a variable say, res to store the decremented value of N with maximum count of trailing 9.
- Initialize a variable say, cntDigits to store the count of digits in N.
- Iterate over the range [1, cntDigits] using variable i and check if N % pow(10, i) greater than or equal to D or not. If found to be true, then print the value of res.
- Otherwise, update res to N % pow(10, i) – 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define ll long long int
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);
}
|
Java
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);
}
}
|
Python3
from math import log10
def maxNumTrailNine(n, d):
res = n
cntDigits = int (log10(n) + 1 )
p10 = 10
for i in range ( 1 , cntDigits + 1 ):
if (n % p10 > = d):
break
else :
res = n - n % p10 - 1
p10 = p10 * 10
print (res)
if __name__ = = '__main__' :
n, d = 1025 , 6
maxNumTrailNine(n, d)
|
C#
using System;
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;
}
Console.WriteLine(res);
}
public static void Main(String[] args)
{
int n = 1025, d = 6;
maxNumTrailNine(n, d);
}
}
|
Javascript
<script>
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);
</script>
|
Time Complexity: O(min(log10(D), log10(N))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...