Given two integers L and R denoting a range [L, R]. The task is to find the total count of numbers in the given range [L,R] whose sum of even digits is greater than the sum of odd digits.
Input : L=2 R=10
Output : 4
Numbers having the property that sum of even
digits is greater than sum of odd digits are: 2, 4, 6, 8
Input : L=2 R=17
Output : 7
Firstly, count the required numbers up to R i.e. in the range [0, R]. To reach the answer in the range [L, R] solve for the range from zero to R and then subtracting the answer for the range from zero to L – 1. Define the DP states as follows:
- Consider the number as a sequence of digits, one state is the position at which we are currently at. This position can have values from 0 to 18 if we are dealing with the numbers up to 10^18. In each recursive call, try to build the sequence from left to right by placing a digit from 0 to 9.
- First state is the sum of the even digits that has been placed so far.
- Second state is the sum of the odd digits that has been placed so far.
- Another state is the boolean variable tight which tells the number we are trying to build has already become smaller than R so that in the upcoming recursive calls we can place any digit from 0 to 9. If the number has not become smaller, the maximum limit of digit we can place is the digit at the current position in R.
Below is the implementation of the above approach:
Time Complexity : There would be at max 18*(180)*(180)*2 computations when 0 < a,b < 1018
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- 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 from range [L, R] that end with any of the given digits
- Count of numbers from range [L, R] whose sum of digits is Y
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Total numbers with no repeated digits in a range
- Cumulative product of digits of all numbers in the given range
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Numbers with a Fibonacci difference between Sum of digits at even and odd positions in a given range
- Find all the possible numbers in a range that can be evenly divided by its digits
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count numbers with same first and last digits
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.