Given a positive integer N, the task is to find the total number of digits in the concatenation of the first N positive integers.
Input: N = 10
The number formed is 12345678910.
Hence, the total number of digits = 11
Input: N = 20
The number formed is 1234567891011121314151617181920
Hence, the total number of digits = 31
Approach: Lets make an observation with the examples.
- Let N = 13. So, the digits present in all the numbers between 1 to 13 at one’s place are 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3.
- Similarly, the digits at the tens place are 1, 1, 1, 1.
- So, total ones place digits from 1 to 13 are 13(13 – 0).
- Similarly, the total tens place digits are 4(13 – 9).
- Now, lets look at another number to understand the pattern. Let N = 234. So, the digits at units place are 1(24 times), 2(24 times), 3(24 times), 4(24 times), 5(23 times), 6(23 times), 7(23 times), 8(23 times), 9(23 times), 0(23 times). Hence, 23 * 6 + 24 * 4 = 234.
- Similarly, the digits at tens place are 234 – 9 = 225 because from 1 to 234, only 1 – 9 are the single-digit numbers.
- Lastly, the number of digits at the hundredths place is 234 – 99 = 225 as from 1 to 234, only 1 – 9 are the single-digit numbers and 1 – 99 are the double-digit numbers.
- Therefore, the total number of digits formed when concatenated is 234(234 – 1 + 1) + 225(234 – 10 + 1) + 135(234 – 100 + 1) = 594.
- Hence, the idea is to subtract 0, 9, 99, 999 …. from N to get the number of digits at every place and the summation of all of this is the required answer.
On generalizing the above pattern, the following formula is generated:
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count positive integers with 0 as a digit and maximum 'd' digits
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Permutation of first N positive integers such that prime numbers are at prime indices
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Print first k digits of 1/n where n is a positive integer
- Count 'd' digit positive integers with 0 as a digit
- Count sequences of positive integers having product X
- Find the number obtained after concatenation of binary representation of M and N
- Pairs whose concatenation contain all digits
- Find the first N integers such that the sum of their digits is equal to 10
- Check if the concatenation of first N natural numbers is divisible by 3
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum digits to be removed to make either all digits or alternating digits same
- Number of arrays of size N whose elements are positive integers and sum is K
- Ways to write N as sum of two or more positive integers | Set-2
- Find n positive integers that satisfy the given equations
- Check whether product of integers from a to b is positive , negative or zero
- Check whether a number can be represented as sum of K distinct positive integers
- Number of ways in which N can be represented as the sum of two positive integers
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.