Largest number with maximum trailing nines which is less than N and greater than N-D

Given two numbers N and D. The task is to find out the largest number smaller than or equal to N which contains the maximum number of trailing nines and the difference between N and the number should not be greater than D.

Examples:

Input: N = 1029, D = 102
Output: 999
1029 has 1 trailing nine while 999 has three 
trailing nine.Also 1029-999 = 30(which is less than 102).

Input: N = 10281, D = 1
Output: 10281


A naive approach will be to iterate from N till N-D and find the number with the largest number of trailing nines.

An efficient approach can be found by some key observations. One key observation for this problem is that the largest number smaller than N ending with at least say(K) nines is

[n – (n MOD 10^k) – 1]

Traverse all possible values of k starting from total no of digits of N to 1, and check whether d > n%10^k. If no such value is obtained, the final answer will be N itself. Otherwise, check for the answer using the above observation.

Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP to implement above function
#include <bits/stdc++.h>
using namespace std;
  
// It's better to use long long
// to handle big integers
#define ll long long
  
// function to count no of digits
ll dig(ll a)
{
    ll count = 0;
    while (a > 0) {
        a /= 10;
        count++;
    }
    return count;
}
  
// function to implement above approach
void required_number(ll num, ll n, ll d)
{
    ll i, j, power, a, flag = 0;
    for (i = num; i >= 1; i--) {
        power = pow(10, i);
        a = n % power;
  
        // if difference between power
        // and n doesn't exceed d
        if (d > a) {
            flag = 1;
            break;
        }
    }
    if (flag) {
        ll t = 0;
  
        // loop to build a number from the
        // appropriate no of digits containg only 9
        for (j = 0; j < i; j++) {
            t += 9 * pow(10, j);
        }
  
        // if the build number is
        // same as original number(n)
        if (n % power == t)
            cout << n;
        else {
  
            // observation
            cout << n - (n % power) - 1;
        }
    }
    else
        cout << n;
}
  
// Driver Code
int main()
{
    ll n = 1029, d = 102;
  
    // variable that stores no of digits in n
    ll num = dig(n);
    required_number(num, n, d);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to implement above function
import java.io.*;
   
class GFG {
      
// It's better to use long 
// to handle big integers
// function to count no. of digits
static long dig(long a)
{
    long count = 0;
    while (a > 0
    {
        a /= 10;
        count++;
    }
    return count;
}
   
// function to implement above approach
 static void required_number(long num, long n, long d)
{
    long i, j, power=1, a, flag = 0;
    for (i = num; i >= 1; i--) 
    {
        power = (long)Math.pow(10, i);
        a = n % power;
   
        // if difference between power
        // and n doesn't exceed d
        if (d > a)
        {
            flag = 1;
            break;
        }
    }
      
    if (flag>0
    {
        long t = 0;
   
        // loop to build a number from the
        // appropriate no of digits containg
        // only 9
        for (j = 0; j < i; j++) 
        {
            t += 9 * Math.pow(10, j);
        }
   
        // if the build number is
        // same as original number(n)
        if (n % power == t)
            System.out.print( n);
              
        else {
   
            // observation
            System.out.print( n - (n % power) - 1);
        }
    }
    else
        System.out.print(n);
}
   
    // Driver Code
    public static void main (String[] args) 
    {
        long n = 1029, d = 102;
       
        // variable that stores no 
        // of digits in n
        long num = dig(n);
        required_number(num, n, d);
    }
}
   
// This code is contributed by chandan_jnu

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 to implement above function
  
# function to count no of digits
def dig(a):
    count = 0;
    while (a > 0):
        a /= 10
        count+=1
    return count
  
  
# function to implement above approach
def required_number(num, n, d):
    flag = 0
    power=0
    a=0
    for i in range(num,0,-1):
        power = pow(10, i)
        a = n % power
          
        # if difference between power
        # and n doesn't exceed d
          
        if (d > a):
            flag = 1
            break
    if(flag):
        t=0
        # loop to build a number from the
        # appropriate no of digits containg only 9
        for j in range(0,i):
            t += 9 * pow(10, j)
          
        # if the build number is
        # same as original number(n)
        if(n % power ==t):
            print(n,end="")
        else:
            # observation
            print((n - (n % power) - 1),end="")
    else:
        print(n,end="")
# Driver Code
  
if __name__ == "__main__":
    n = 1029
    d = 102
  
# variable that stores no of digits in n
    num = dig(n)
    required_number(num, n, d)
  
# this code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to implement
// above function
using System;
  
class GFG 
{
      
// It's better to use long 
// to handle big integers
// function to count no. of digits
static long dig(long a)
{
    long count = 0;
    while (a > 0) 
    {
        a /= 10;
        count++;
    }
    return count;
}
  
// function to implement
// above approach
static void required_number(long num, 
                            long n, 
                            long d)
{
    long i, j, power = 1, a, flag = 0;
    for (i = num; i >= 1; i--) 
    {
        power = (long)Math.Pow(10, i);
        a = n % power;
  
        // if difference between power
        // and n doesn't exceed d
        if (d > a)
        {
            flag = 1;
            break;
        }
    }
      
    if (flag > 0) 
    {
        long t = 0;
  
        // loop to build a number 
        // from the appropriate no 
        // of digits containg only 9
        for (j = 0; j < i; j++) 
        {
            t += (long)(9 * Math.Pow(10, j));
        }
  
        // if the build number is
        // same as original number(n)
        if (n % power == t)
            Console.Write( n);
              
        else 
        {
  
            // observation
            Console.Write(n - (n % power) - 1);
        }
    }
    else
        Console.Write(n);
}
  
    // Driver Code
    public static void Main() 
    {
        long n = 1029, d = 102;
      
        // variable that stores 
        // no. of digits in n
        long num = dig(n);
        required_number(num, n, d);
    }
}
  
// This code is contributed
// by chandan_jnu

chevron_right


PHP

0)
{
$a = (int)($a / 10);
$count++;
}
return $count;
}

// function to implement above approach
function required_number($num, $n, $d)
{
$flag = 0;
for ($i = $num; $i >= 1; $i–)
{
$power = pow(10, $i);
$a = $n % $power;

// if difference between power
// and n doesn’t exceed d
if ($d > $a)
{
$flag = 1;
break;
}
}
if ($flag)
{
$t = 0;

// loop to build a number from the
// appropriate no of digits containg only 9
for ($j = 0; $j < $i; $j++) { $t += 9 * pow(10, $j); } // if the build number is // same as original number(n) if ($n % $power == $t) echo $n; else { // observation echo ($n - ($n % $power) - 1); } } else echo $n; } // Driver Code $n = 1029; $d = 102; // variable that stores no of // digits in n $num = dig($n); required_number($num, $n, $d); // This code is contributed by mits ?>

Output:

999

Time Complexity:O(no of digits)



My Personal Notes arrow_drop_up

Dream it Do it

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.