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
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 of integers in a range which have even number of odd digits and odd number of even digits
- Check if a number has an odd count of odd divisors and even count of even divisors
- Smallest subarray of size greater than K with sum greater than a given value
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Numbers with a Fibonacci difference between Sum of digits at even and odd positions in a given range
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Count of permutations such that sum of K numbers from given range is even
- Largest number not greater than N all the digits of which are odd
- Count Odd and Even numbers in a range from L to R
- Check whether XOR of all numbers in a given range is even or odd
- Print even and odd numbers in a given range using recursion
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Sum of range in a series of first odd then even natural numbers
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Minimise N such that sum of count of all factors upto N is greater than or equal to X
- Length of Smallest subarray in range 1 to N with sum greater than a given value
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.