Given an integer K and a range of consecutive numbers [L, R]. The task is to count the numbers from the given range which have digital root as K (1 ≤ K ≤ 9). Digital root is sum of digits of a number until it becomes a single digit number. For example, 256 -> 2 + 5 + 6 = 13 -> 1 + 3 = 4.
Input: L = 10, R = 22, K = 3
12 and 21 are the only numbers from the range whose digit sum is 3.
Input: L = 100, R = 200, K = 5
- First thing is to note that for any number Sum of Digits is equal to Number % 9. If remainder is 0, then sum of digits is 9.
- So if K = 9, then replace K with 0.
- Task, now is to find count of numbers in range L to R with modulo 9 equal to K.
- Divide the entire range into the maximum possible groups of 9 starting with L (TotalRange / 9), since in each range there will be exactly one number with modulo 9 equal to K.
- Loop over rest number of elements from R to R – count of rest elements, and check if any number satisfies the condition.
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.
- Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer
- Digital Root (repeated digital sum) of the given large integer
- Digital Root of a given large integer using Recursion
- Sudo Placement[1.7] | Greatest Digital Root
- Print a number containing K digits with digital root D
- Find Nth positive number whose digital root is X
- Smallest root of the equation x^2 + s(x)*x - n = 0, where s(x) is the sum of digits of root x.
- Check if product of Array elements in given range are M-th root or not
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Count numbers from a given range that contains a given number as the suffix
- Find square root of number upto given precision using binary search
- Find the root of given non decreasing function between A and B
- C program to find square root of a given number
- Least root of given quadratic equation for value greater than equal to K
- Queries to increment array elements in a given range by a given value for a given number of times
- Generate a pair of integers from a range [L, R] whose LCM also lies within the range
- Count factorial numbers in a given range
- Print all Good numbers in given range
- Find numbers with K odd divisors in a given range
- Print prime numbers in a given range using C++ STL
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.