Given an interval, the task is to count numbers which have same first and last digits. For example, 1231 has same first and last digits.
Input : start = 7, end : 68 Output : 9 Number with same first and last digits are, 7 8 9 11 22 33 44 55 66. Input : start = 5, end : 40 Output : 8
Let us first consider below examples to understand the approach.
From 120 to 130, only 121 has same starting and ending digit From 440 to 450, only 444 has same starting and ending digit From 1050 to 1060, only 1051 has same starting and ending digit
From above examples, we can observe that in each ten number span we have only one number which has the given property except 1 to 10 which has 9 numbers (all one digit number) having same starting and ending digit.
Using above property we can solve given problem, first we break the given interval into two parts that is if interval is l to r, we break this into 1 to l and 1 to r, our answer is obtained by subtracting result of first query from second query.
Now we remain to find count of numbers with given property in range 1 to x, for this we divide x by 10, which gives number of 10-spans. We add 9 to the span for taking one digit numbers into account. If last digit of x is smaller than first digit of x, then 1 should be decreased in final answer to discard last ten span number because that number is out of range.
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count of numbers from range [L, R] that end with any of the given digits
- Count of numbers with all digits same in a given range
- Count of numbers from range [L, R] whose sum of digits is Y
- Count the numbers with N digits and whose suffix is divisible by K
- Count different numbers possible using all the digits their frequency times
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count numbers formed by given two digit with sum having given digits
- Count of N-digit numbers with all distinct digits
- Count of n digit numbers whose sum of digits equals to given sum
- Count numbers with exactly K non-zero digits and distinct odd digit sum
- Count of Octal numbers upto N digits
- Count Numbers with N digits which consists of odd number of 0's
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count different numbers that can be generated such that there digits sum is equal to 'n'
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count Numbers with N digits which consists of even number of 0’s
- Count of numbers in Array ending with digits of number N
Improved By : nitin mittal