Given a range [L, R]. The task is to count the numbers in the range having difference between the sum of digits at even position and sum of digits at odd position is a Prime Number. Consider the position of least significant digit in the number as an odd position.
Input : L = 1, R = 50 Output : 6 Explanation : Only, 20, 30, 31, 41, 42 and 50 are valid numbers. Input : L = 50, R = 100 Output : 18
Prerequisites :Digit DP
Approach : Firstly, if we are able to count the required numbers up to R i.e. in the range [0, R], we can easily reach our answer in the range [L, R] by solving for from zero to R and then subtracting the answer we get after solving from zero to L – 1. Now, we need to define the DP states.
- Since we can consider our 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 1018. In each recursive call, we try to build the sequence from left to right by placing a digit from 0 to 9.
- First state is the sum of the digits at even positions we have placed so far.
- Second state is the sum of the digits at odd positions we have 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 digit at the current position in R.
Also, when we reach the base condition, we need to check whether the required difference is a prime number or not. Since the highest number in range is 1018, the maximum sum at either even or odd positions can be at max 9 times 9 and hence the maximum difference. So, we need to check only prime numbers only upto 100 at base condition.
Below is the implementation of the above approach:
- Numbers with a Fibonacci difference between Sum of digits at even and odd positions in a given range
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Check if a number has an odd count of odd divisors and even count of even divisors
- Count subarrays having sum of elements at even and odd positions equal
- Count all prime numbers in a given range whose sum of digits is also prime
- Count of Numbers in a Range divisible by m and having digit d in even positions
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
- Sum of prime numbers without odd prime digits
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Count Odd and Even numbers in a range from L to R
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.