# Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime

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.

**Examples:**

Input :L = 1, R = 50Output :6Explanation :Only, 20, 30, 31, 41, 42 and 50 are valid numbers.Input :L = 50, R = 100Output :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.

**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 10^{18}. 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 10^{18}, 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:

## C++

`// C++ implementation of the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `const` `int` `M = 18; ` `int` `a, b, dp[M][90][90][2]; ` ` ` `// Prime numbers upto 100 ` `int` `prime[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, ` ` ` `29, 31, 37, 43, 47, 53, 59, 61, ` ` ` `67, 71, 73, 79, 83, 89, 97 }; ` ` ` `// Function to return the count of ` `// required numbers from 0 to num ` `int` `count(` `int` `pos, ` `int` `even, ` `int` `odd, ` `int` `tight, ` ` ` `vector<` `int` `> num) ` `{ ` ` ` `// Base Case ` ` ` `if` `(pos == num.size()) { ` ` ` `if` `(num.size() & 1) ` ` ` `swap(odd, even); ` ` ` `int` `d = even - odd; ` ` ` ` ` `// check if the difference is equal ` ` ` `// to any prime number ` ` ` `for` `(` `int` `i = 0; i < 24; i++) ` ` ` `if` `(d == prime[i]) ` ` ` `return` `1; ` ` ` ` ` `return` `0; ` ` ` `} ` ` ` ` ` `// If this result is already computed ` ` ` `// simply return it ` ` ` `if` `(dp[pos][even][odd][tight] != -1) ` ` ` `return` `dp[pos][even][odd][tight]; ` ` ` ` ` `int` `ans = 0; ` ` ` ` ` `// Maximum limit upto which we can place ` ` ` `// digit. If tight is 1, means number has ` ` ` `// already become smaller so we can place ` ` ` `// any digit, otherwise num[pos] ` ` ` `int` `limit = (tight ? 9 : num[pos]); ` ` ` ` ` `for` `(` `int` `d = 0; d <= limit; d++) { ` ` ` `int` `currF = tight, currEven = even; ` ` ` `int` `currOdd = odd; ` ` ` ` ` `if` `(d < num[pos]) ` ` ` `currF = 1; ` ` ` ` ` `// If the current position is odd ` ` ` `// add it to currOdd, otherwise to ` ` ` `// currEven ` ` ` `if` `(pos & 1) ` ` ` `currOdd += d; ` ` ` `else` ` ` `currEven += d; ` ` ` ` ` `ans += count(pos + 1, currEven, currOdd, ` ` ` `currF, num); ` ` ` `} ` ` ` ` ` `return` `dp[pos][even][odd][tight] = ans; ` `} ` ` ` `// Function to convert x into its digit vector ` `// and uses count() function to return the ` `// required count ` `int` `solve(` `int` `x) ` `{ ` ` ` `vector<` `int` `> num; ` ` ` ` ` `while` `(x) { ` ` ` `num.push_back(x % 10); ` ` ` `x /= 10; ` ` ` `} ` ` ` ` ` `reverse(num.begin(), num.end()); ` ` ` ` ` `// Initialize dp ` ` ` `memset` `(dp, -1, ` `sizeof` `(dp)); ` ` ` `return` `count(0, 0, 0, 0, num); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `L = 1, R = 50; ` ` ` `cout << solve(R) - solve(L - 1) << endl; ` ` ` ` ` `L = 50, R = 100; ` ` ` `cout << solve(R) - solve(L - 1) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `M = ` `18` `; ` `static` `int` `a, b, dp[][][][] = ` `new` `int` `[M][` `90` `][` `90` `][` `2` `]; ` ` ` `// Prime numbers upto 100 ` `static` `int` `prime[] = { ` `2` `, ` `3` `, ` `5` `, ` `7` `, ` `11` `, ` `13` `, ` `17` `, ` `19` `, ` `23` `, ` ` ` `29` `, ` `31` `, ` `37` `, ` `43` `, ` `47` `, ` `53` `, ` `59` `, ` `61` `, ` ` ` `67` `, ` `71` `, ` `73` `, ` `79` `, ` `83` `, ` `89` `, ` `97` `}; ` ` ` `// Function to return the count of ` `// required numbers from 0 to num ` `static` `int` `count(` `int` `pos, ` `int` `even, ` `int` `odd, ` `int` `tight, ` ` ` `Vector<Integer> num) ` `{ ` ` ` `// Base Case ` ` ` `if` `(pos == num.size()) ` ` ` `{ ` ` ` `if` `((num.size() & ` `1` `) != ` `0` `) ` ` ` `{ ` ` ` `int` `t = odd; ` ` ` `odd = even; ` ` ` `even = t; ` ` ` ` ` `} ` ` ` `int` `d = even - odd; ` ` ` ` ` `// check if the difference is equal ` ` ` `// to any prime number ` ` ` `for` `(` `int` `i = ` `0` `; i < ` `24` `; i++) ` ` ` `if` `(d == prime[i]) ` ` ` `return` `1` `; ` ` ` ` ` `return` `0` `; ` ` ` `} ` ` ` ` ` `// If this result is already computed ` ` ` `// simply return it ` ` ` `if` `(dp[pos][even][odd][tight] != -` `1` `) ` ` ` `return` `dp[pos][even][odd][tight]; ` ` ` ` ` `int` `ans = ` `0` `; ` ` ` ` ` `// Maximum limit upto which we can place ` ` ` `// digit. If tight is 1, means number has ` ` ` `// already become smaller so we can place ` ` ` `// any digit, otherwise num[pos] ` ` ` `int` `limit = (tight != ` `0` `? ` `9` `: num.get(pos)); ` ` ` ` ` `for` `(` `int` `d = ` `0` `; d <= limit; d++) ` ` ` `{ ` ` ` `int` `currF = tight, currEven = even; ` ` ` `int` `currOdd = odd; ` ` ` ` ` `if` `(d < num.get(pos)) ` ` ` `currF = ` `1` `; ` ` ` ` ` `// If the current position is odd ` ` ` `// add it to currOdd, otherwise to ` ` ` `// currEven ` ` ` `if` `((pos & ` `1` `) != ` `0` `) ` ` ` `currOdd += d; ` ` ` `else` ` ` `currEven += d; ` ` ` ` ` `ans += count(pos + ` `1` `, currEven, currOdd, ` ` ` `currF, num); ` ` ` `} ` ` ` ` ` `return` `dp[pos][even][odd][tight] = ans; ` `} ` ` ` `// Function to convert x into its digit vector ` `// and uses count() function to return the ` `// required count ` `static` `int` `solve(` `int` `x) ` `{ ` ` ` `Vector<Integer> num = ` `new` `Vector<Integer>(); ` ` ` ` ` `while` `(x != ` `0` `) ` ` ` `{ ` ` ` `num.add(x % ` `10` `); ` ` ` `x /= ` `10` `; ` ` ` `} ` ` ` ` ` `Collections.reverse(num); ` ` ` ` ` `// Initialize dp ` ` ` `for` `(` `int` `i = ` `0` `; i < dp.length; i++) ` ` ` `for` `(` `int` `j = ` `0` `; j < dp[i].length; j++) ` ` ` `for` `(` `int` `k = ` `0` `; k < dp[i][j].length; k++) ` ` ` `for` `(` `int` `k1 = ` `0` `; k1 < dp[i][j][k].length; k1++) ` ` ` `dp[i][j][k][k1] = -` `1` `; ` ` ` ` ` `return` `count(` `0` `, ` `0` `, ` `0` `, ` `0` `, num); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `L = ` `1` `, R = ` `50` `; ` ` ` `System.out.println( solve(R) - solve(L - ` `1` `)); ` ` ` ` ` `L = ` `50` `; R = ` `100` `; ` ` ` `System.out.println( solve(R) - solve(L - ` `1` `)); ` `} ` `} ` ` ` `// This code is contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

**Output:**

6 18

## Recommended Posts:

- Queries for the difference between the count of composite and prime numbers in a given range
- Count of Numbers in a Range divisible by m and having digit d in even positions
- 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] whose sum of digits is Y
- Count numbers from range whose prime factors are only 2 and 3
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Print prime numbers with prime sum of digits in an array
- Numbers with sum of digits equal to the sum of digits of its all prime factor

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.