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 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++
#include <stdio.h>
int printMaxN( int m)
{
int total_numbers_within_range = 9;
int number_of_digits = 1;
while (m > total_numbers_within_range * number_of_digits) {
m = m - (total_numbers_within_range * number_of_digits);
number_of_digits++;
total_numbers_within_range *= 10;
}
int ans = (total_numbers_within_range) / 9 - 1;
ans += (m / number_of_digits);
return ans;
}
int main()
{
int m = 15;
printf ( "%dn" , printMaxN(m));
return 0;
}
|
Java
import java.util.*;
class GFG {
static int printMaxN( int m)
{
int total_numbers_within_range = 9 ;
int number_of_digits = 1 ;
while (m > total_numbers_within_range * number_of_digits) {
m = m - (total_numbers_within_range * number_of_digits);
number_of_digits++;
total_numbers_within_range *= 10 ;
}
int ans = (total_numbers_within_range) / 9 - 1 ;
ans += (m / number_of_digits);
return ans;
}
public static void main(String[] args)
{
int m = 15 ;
System.out.print(printMaxN(m));
}
}
|
Python3
def printMaxN(m):
total_numbers_within_range = 9
number_of_digits = 1
while (m > total_numbers_within_range * number_of_digits):
m = m - (total_numbers_within_range * number_of_digits)
number_of_digits = number_of_digits + 1
total_numbers_within_range = total_numbers_within_range * 10
ans = (total_numbers_within_range) / / 9 - 1
ans = ans + (m / / number_of_digits)
return ans
m = 15
print (printMaxN(m))
|
C#
using System;
class GFG {
static int printMaxN( int m)
{
int total_numbers_within_range = 9;
int number_of_digits = 1;
while (m > total_numbers_within_range * number_of_digits) {
m = m - (total_numbers_within_range * number_of_digits);
number_of_digits++;
total_numbers_within_range *= 10;
}
int ans = (total_numbers_within_range) / 9 - 1;
ans += (m / number_of_digits);
return ans;
}
public static void Main()
{
int m = 15;
Console.Write(printMaxN(m));
}
}
|
PHP
<?php
function printMaxN( $m )
{
$total_numbers_within_range = 9;
$number_of_digits = 1;
while ( $m > $total_numbers_within_range *
$number_of_digits )
{
$m = $m - ( $total_numbers_within_range *
$number_of_digits );
$number_of_digits ++;
$total_numbers_within_range *= 10;
}
$ans = ( $total_numbers_within_range )
/ 9 - 1;
$ans += ( $m / $number_of_digits );
return $ans ;
}
$m = 15;
echo printMaxN( $m );
?>
|
Javascript
<script>
function printMaxN(m)
{
let total_numbers_within_range = 9;
let number_of_digits = 1;
while (m >
total_numbers_within_range * number_of_digits)
{
m = m -
(total_numbers_within_range * number_of_digits);
number_of_digits++;
total_numbers_within_range *= 10;
}
let ans =
parseInt((total_numbers_within_range) / 9, 10) - 1;
ans += parseInt(m / number_of_digits, 10);
return ans;
}
let m = 15;
document.write(printMaxN(m));
</script>
|
Output:
12
Reference :
https://www.geeksforgeeks.org/problems/faulty-keyboard/0
Last Updated :
26 Apr, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...