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 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++
// 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 ?> |
Javascript
<script> // 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 let total_numbers_within_range = 9; let 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 let ans = parseInt((total_numbers_within_range) / 9, 10) - 1; // Add the add the remaining digits/(number of // digits required for current series) ans += parseInt(m / number_of_digits, 10); return ans; } let m = 15; document.write(printMaxN(m)); </script> |
Output:
12
Reference :
https://www.neveropen.co.uk/problems/faulty-keyboard/0
If you like neveropen and would like to contribute, you can also write an article using contribute.neveropen.co.uk or mail your article to review-team@neveropen.co.uk. See your article appearing on the neveropen 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.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!