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.
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:
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 email@example.com. 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.
- 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