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++
// 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; } |
Java
// 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. |
Python3
# 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. |
C#
// 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. |
PHP
<?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 ?> |
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.
Recommended Posts:
- Minimum number of operations to move all uppercase characters before all lower case characters
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Number of compositions of a natural number
- Sum of all proper divisors of a natural number
- Find sum of modulo K of first N natural number
- Find all divisors of a natural number | Set 2
- Find if given number is sum of first n natural numbers
- Find all divisors of a natural number | Set 1
- Find the first natural number whose factorial is divisible by x
- Find the k-th smallest divisor of a natural number N
- Number of pairs from the first N natural numbers whose sum is divisible by K
- Expressing a fraction as a natural number under modulo 'm'
- Sum of first N natural numbers by taking powers of 2 as negative number
- Number of distinct prime factors of first n natural numbers
- Greatest divisor which divides all natural number in range [L, R]
Improved By : jit_t



