Largest natural number that can be printed with M characters

Given a number M which represents maximum number of characters (ignoring spaces) to be used to print first N natural numbers. Find the largest value of N.

Examples:

Input : M = 5
Output : 5
We can type 1 2 3 4 5 using 5 key
presses.

Input : M = 15
Output : 12
We can type 1 2 3 4 5 6 7 8 9 10 
11 12 using 15 key presses.

Observe, for M less than 11 we can print 1 to 9. Therefore, N will be 9. Now for numbers, from 10 to 99 (total 90 numbers), we need two characters. For number from 100 to 999 (total 900 numbers), we need three characters. So, keep on calculating and subtracting number number of characters from M. Also, when M is less than number of characters typed. Find offset numbers that can be print using remaining key press allowed.

Below is the implementation of this approach:

C/C++



filter_none

edit
close

play_arrow

link
brightness_4
code

// Maximum natural number that can be printed
// with M characters.
#include <stdio.h>
  
int printMaxN(int m)
{
    // At starting point, from 1 to 9, we
    // will have only one digit
    int total_numbers_within_range = 9;
    int number_of_digits = 1;
  
    // While the number of characters is
    // greater than the total number of
    // natural number in given range e.g.
    // if m = 12, then at first step, (m >
    // (9)*(1)) evaluates to true
    while (m > total_numbers_within_range * number_of_digits) {
  
        // Now here we have exhausted some
        // of the digits in making up some natural
        // numbers, we reduce the count of m
  
        m = m - (total_numbers_within_range * number_of_digits);
  
        // Increment the number of digits
        number_of_digits++;
  
        // Increase the range of the digits
        total_numbers_within_range *= 10;
    }
  
    // Gives the starting point of any range
    int ans = (total_numbers_within_range) / 9 - 1;
  
    // Add the add the remaining digits/(number of
    // digits required for current series)
    ans += (m / number_of_digits);
  
    return ans;
}
  
// Driver code
int main()
{
    int m = 15;
    printf("%dn", printMaxN(m));
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Maximum natural number that
// can be printed with M characters.
import java.util.*;
  
class GFG {
  
    static int printMaxN(int m)
    {
  
        // At starting point, from 1 to 9, we
        // will have only one digit
        int total_numbers_within_range = 9;
        int number_of_digits = 1;
  
        // While the number of characters is
        // greater than the total number of
        // natural number in given range e.g.
        // if m = 12, then at first step, (m >
        // (9)*(1)) evaluates to true
        while (m > total_numbers_within_range * number_of_digits) {
  
            // Now here we have exhausted some
            // of the digits in making up some natural
            // numbers, we reduce the count of m
  
            m = m - (total_numbers_within_range * number_of_digits);
  
            // Increment the number of digits
            number_of_digits++;
  
            // Increase the range of the digits
            total_numbers_within_range *= 10;
        }
  
        // Gives the starting point of any range
        int ans = (total_numbers_within_range) / 9 - 1;
  
        // Add the add the remaining digits/(number of
        // digits required for current series)
        ans += (m / number_of_digits);
  
        return ans;
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        int m = 15;
        System.out.print(printMaxN(m));
    }
}
  
// This code is contributed by Anant Agarwal.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Maximum natural number
# that can be printed
# with M characters.
  
def printMaxN(m):
  
    # At starting point, from 1 to 9, we 
    # will have only one digit
    total_numbers_within_range = 9
    number_of_digits = 1
    
    ''' While the number of characters is 
        greater than the total number of 
        natural number in given range e.g.''' 
       # if m = 12, then at first step, (m > 
      # (9)*(1)) evaluates to true
    while (m > total_numbers_within_range * number_of_digits):
   
        # Now here we have exhausted some 
        # of the digits in making up some natural 
        # 3 numbers, we reduce the count of m
    
        m = m - (total_numbers_within_range * number_of_digits)
    
        # Increment the number of digits
        number_of_digits = number_of_digits + 1
    
        # Increase the range of the digits
        total_numbers_within_range = total_numbers_within_range * 10
      
    
    # Gives the starting point of any range
    ans = (total_numbers_within_range) // 9 - 1
    
    # Add the add the remaining digits/(number of
    # digits required for current series)
    ans = ans +  (m // number_of_digits)
    
    return ans
  
# Driver code
  
m = 15 
print(printMaxN(m))
  
  
# This code is contributed
# by Anant Agarwal.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Maximum natural number that
// can be printed with M characters.
using System;
  
class GFG {
  
    static int printMaxN(int m)
    {
  
        // At starting point, from 1 to 9, we
        // will have only one digit
        int total_numbers_within_range = 9;
        int number_of_digits = 1;
  
        // While the number of characters is
        // greater than the total number of
        // natural number in given range e.g.
        // if m = 12, then at first step, (m >
        // (9)*(1)) evaluates to true
        while (m > total_numbers_within_range * number_of_digits) {
  
            // Now here we have exhausted some
            // of the digits in making up some natural
            // numbers, we reduce the count of m
  
            m = m - (total_numbers_within_range * number_of_digits);
  
            // Increment the number of digits
            number_of_digits++;
  
            // Increase the range of the digits
            total_numbers_within_range *= 10;
        }
  
        // Gives the starting point of any range
        int ans = (total_numbers_within_range) / 9 - 1;
  
        // Add the add the remaining digits/(number of
        // digits required for current series)
        ans += (m / number_of_digits);
  
        return ans;
    }
  
    // Driver code
    public static void Main()
    {
        int m = 15;
        Console.Write(printMaxN(m));
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program for Maximum 
// natural number that can
// be printed with M 
// characters.
  
function printMaxN($m)
{
      
    // At starting point, 
    // from 1 to 9, we 
    // will have only one digit
    $total_numbers_within_range = 9;
    $number_of_digits = 1;
  
    // While the number of characters is 
    // greater than the total number of 
    // natural number in given range e.g. 
    // if m = 12, then at first step, (m > 
    // (9)*(1)) evaluates to true
    while ($m > $total_numbers_within_range *
                          $number_of_digits
    {
  
        // Now here we have 
        // exhausted some of 
        // the digits in making
        // up some natural numbers,
        // we reduce the count of m
        $m = $m - ($total_numbers_within_range *
                             $number_of_digits);
  
        // Increment the number
        // of digits
        $number_of_digits++;
  
        // Increase the range 
        // of the digits
        $total_numbers_within_range *= 10;
    }
  
    // Gives the starting 
    // point of any range
    $ans = ($total_numbers_within_range)
                                / 9 - 1;
  
    // Add the add the remaining
    // digits/(number of digits 
    // required for current series)
    $ans += ($m / $number_of_digits);
  
    return $ans;
}
  
    // Driver Code
    $m = 15; 
    echo printMaxN($m);
// This code is contributed by ajit
?>

chevron_right



Output:

12

Reference :
https://practice.geeksforgeeks.org/problems/faulty-keyboard/0

This article is contributed by Anuj Chauhan. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : jit_t



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.