Prerequisite : How to solve a Dynamic Programming Problem ?
There are many types of problems that ask to count the number of integers ‘x‘ between two integers say ‘a‘ and ‘b‘ such that x satisfies a specific property that can be related to its digits.
So, if we say G(x) tells the number of such integers between 1 to x (inclusively), then the number of such integers between a and b can be given by G(b) – G(a-1). This is when Digit DP (Dynamic Programming) comes into action. All such integer counting problems that satisfy the above property can be solved by digit DP approach.
Key Concept:
- Let given number x has n digits. The main idea of digit DP is to first represent the digits as an array of digits t[]. Let’s say a we have tntn-1tn-2 … t2t1 as the decimal representation where ti (0 < i <= n) tells the i-th digit from the right. The leftmost digit tn is the most significant digit.
- Now, after representing the given number this way we generate the numbers less than the given number and simultaneously calculate using DP, if the number satisfy the given property. We start generating integers having number of digits = 1 and then till number of digits = n. Integers having less number of digits than n can be analyzed by setting the leftmost digits to be zero.
Example Problem :
Given two integers a and b. Your task is to print the sum of
all the digits appearing in the integers between a and b.
For example if a = 5 and b = 11, then answer is 38 (5 + 6 + 7 + 8 + 9 + 1 + 0 + 1 + 1)
Constraints : 1 <= a < b <= 10^18
Now we see that if we have calculated the answer for state having n-1 digits, i.e., tn-1 tn-2 … t2 t1 and we need to calculate answer for state having n digits tn tn-1 tn-2 … t2 t1. So, clearly, we can use the result of the previous state instead of re-calculating it. Hence, it follows the overlapping property.
Let’s think for a state for this DP
Our DP state will be dp(idx, tight, sum)
1) idx
- It tells about the index value from right in the given integer
2) tight
- This will tell if the current digits range is restricted or not. If the current digit’s
range is not restricted then it will span from 0 to 9 (inclusively) else it will span
from 0 to digit[idx] (inclusively).
Example: consider our limiting integer to be 3245 and we need to calculate G(3245)
index : 4 3 2 1
digits : 3 2 4 5
Unrestricted range:
Now suppose the integer generated till now is : 3 1 * * ( * is empty place, where digits are to be inserted to form the integer).
index : 4 3 2 1
digits : 3 2 4 5
generated integer: 3 1 _ _
here, we see that index 2 has unrestricted range. Now index 2 can have digits from range 0 to 9(inclusively).
For unrestricted range tight = 0
Restricted range:
Now suppose the integer generated till now is : 3 2 * * ( ‘*’ is an empty place, where digits are to be inserted to form the integer).
index : 4 3 2 1
digits : 3 2 4 5
generated integer: 3 2 _ _
here, we see that index 2 has a restricted range. Now index 2 can only have digits from range 0 to 4 (inclusively)
For restricted range tight = 1
3) sum
- This parameter will store the sum of digits in the generated integer from msd to idx.
- Max value for this parameter sum can be 9*18 = 162, considering 18 digits in the integer
State Relation:
The basic idea for state relation is very simple. We formulate the dp in top-down fashion.
Let’s say we are at the msd having index idx. So initially the sum will be 0.
Therefore, we will fill the digit at index by the digits in its range. Let’s say its range is from 0 to k (k<=9, depending on the tight value) and fetch the answer from the next state having index = idx-1 and sum = previous sum + digit chosen.
int ans = 0;
for (int i=0; i<=k; i++) {
ans += state(idx-1, newTight, sum+i)
}
state(idx,tight,sum) = ans;
How to calculate the newTight value?
The new tight value from a state depends on its previous state. If tight value form the previous state is 1 and the digit at idx chosen is digit[idx](i.e the digit at idx in limiting integer) , then only our new tight will be 1 as it only then tells that the number formed till now is prefix of the limiting integer.
// digitTaken is the digit chosen
// digit[idx] is the digit in the limiting
// integer at index idx from right
// previouTight is the tight value form previous
// state
newTight = previousTight & (digitTake == digit[idx])
Below is the implementation of the above approach
CPP
#include "bits/stdc++.h"
using namespace std;
long long dp[20][180][2];
void getDigits( long long x, vector < int > &digit)
{
while (x)
{
digit.push_back(x%10);
x /= 10;
}
}
long long digitSum( int idx, int sum, int tight,
vector < int > &digit)
{
if (idx == -1)
return sum;
if (dp[idx][sum][tight] != -1 and tight != 1)
return dp[idx][sum][tight];
long long ret = 0;
int k = (tight)? digit[idx] : 9;
for ( int i = 0; i <= k ; i++)
{
int newTight = (digit[idx] == i)? tight : 0;
ret += digitSum(idx-1, sum+i, newTight, digit);
}
if (!tight)
dp[idx][sum][tight] = ret;
return ret;
}
int rangeDigitSum( int a, int b)
{
memset (dp, -1, sizeof (dp));
vector< int > digitA;
getDigits(a-1, digitA);
long long ans1 = digitSum(digitA.size()-1, 0, 1, digitA);
vector< int > digitB;
getDigits(b, digitB);
long long ans2 = digitSum(digitB.size()-1, 0, 1, digitB);
return (ans2 - ans1);
}
int main()
{
long long a = 123, b = 1024;
cout << "digit sum for given range : "
<< rangeDigitSum(a, b) << endl;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.Arrays;
public class GFG {
static long dp[][][] = new long [ 20 ][ 180 ][ 2 ];
static void getDigits( long x, ArrayList<Integer> digit)
{
while (x != 0 ) {
digit.add(( int )(x % 10 ));
x /= 10 ;
}
}
static long digitSum( int idx, int sum, int tight,
ArrayList<Integer> digit)
{
if (idx == - 1 )
return sum;
if (dp[idx][sum][tight] != - 1 && tight != 1 )
return dp[idx][sum][tight];
long ret = 0 ;
int k = (tight != 0 ) ? digit.get(idx) : 9 ;
for ( int i = 0 ; i <= k; i++) {
int newTight
= (digit.get(idx) == i) ? tight : 0 ;
ret += digitSum(idx - 1 , sum + i, newTight,
digit);
}
if (tight != 0 )
dp[idx][sum][tight] = ret;
return ret;
}
static int rangeDigitSum( int a, int b)
{
for ( int i = 0 ; i < 20 ; i++)
for ( int j = 0 ; j < 180 ; j++)
for ( int k = 0 ; k < 2 ; k++)
dp[i][j][k] = - 1 ;
ArrayList<Integer> digitA
= new ArrayList<Integer>();
getDigits(a - 1 , digitA);
long ans1
= digitSum(digitA.size() - 1 , 0 , 1 , digitA);
ArrayList<Integer> digitB
= new ArrayList<Integer>();
getDigits(b, digitB);
long ans2
= digitSum(digitB.size() - 1 , 0 , 1 , digitB);
return ( int )(ans2 - ans1);
}
public static void main(String[] args)
{
int a = 123 , b = 1024 ;
System.out.println( "digit sum for given range : "
+ rangeDigitSum(a, b));
}
}
|
Python3
dp = [[[ - 1 for i in range ( 2 )] for j in range ( 180 )] for k in range ( 20 )]
def getDigits(x, digit):
while x:
digit.append(x % 10 )
x / / = 10
def digitSum(index, sumof, tight, digit):
if index = = - 1 :
return sumof
if dp[index][sumof][tight] ! = - 1 and tight ! = 1 :
return dp[index][sumof][tight]
ret = 0
k = digit[index] if tight else 9
for i in range ( 0 , k + 1 ):
newTight = tight if digit[index] = = i else 0
ret + = digitSum(index - 1 , sumof + i, newTight, digit)
if not tight:
dp[index][sumof][tight] = ret
return ret
def rangeDigitSum(a, b):
digitA = []
getDigits(a - 1 , digitA)
ans1 = digitSum( len (digitA) - 1 , 0 , 1 , digitA)
digitB = []
getDigits(b, digitB)
ans2 = digitSum( len (digitB) - 1 , 0 , 1 , digitB)
return ans2 - ans1
a, b = 123 , 1024
print ( "digit sum for given range: " , rangeDigitSum(a, b))
|
C#
using System;
using System.Collections.Generic;
namespace GFG {
class Program {
static long [, , ] dp = new long [20, 180, 2];
static void getDigits( long x, List< int > digit)
{
while (x != 0) {
digit.Add(( int )(x % 10));
x /= 10;
}
}
static long digitSum( int idx, int sum, int tight,
List< int > digit)
{
if (idx == -1)
return sum;
if (dp[idx, sum, tight] != -1 && tight != 1)
return dp[idx, sum, tight];
long ret = 0;
int k = (tight != 0) ? digit[idx] : 9;
for ( int i = 0; i <= k; i++) {
int newTight = (digit[idx] == i) ? tight : 0;
ret += digitSum(idx - 1, sum + i, newTight,
digit);
}
if (tight != 0)
dp[idx, sum, tight] = ret;
return ret;
}
static int rangeDigitSum( int a, int b)
{
for ( int i = 0; i < 20; i++)
for ( int j = 0; j < 180; j++)
for ( int k = 0; k < 2; k++)
dp[i, j, k] = -1;
List< int > digitA = new List< int >();
getDigits(a - 1, digitA);
long ans1
= digitSum(digitA.Count - 1, 0, 1, digitA);
List< int > digitB = new List< int >();
getDigits(b, digitB);
long ans2
= digitSum(digitB.Count - 1, 0, 1, digitB);
return ( int )(ans2 - ans1);
}
public static void Main(String[] args)
{
int a = 123, b = 1024;
Console.WriteLine( "digit sum for given range : "
+ rangeDigitSum(a, b));
}
}
}
|
Javascript
let dp = [];
function getDigits(x, digit) {
while (x) {
digit.push(x % 10);
x = Math.floor(x / 10);
}
}
function digitSum(idx, sum, tight, digit) {
if (idx === -1) return sum;
if (dp[idx] && dp[idx][sum] && dp[idx][sum][tight] != null && tight !== 1) {
return dp[idx][sum][tight];
}
let ret = 0;
let k = tight ? digit[idx] : 9;
for (let i = 0; i <= k; i++) {
let newTight = digit[idx] === i ? tight : 0;
ret += digitSum(idx - 1, sum + i, newTight, digit);
}
if (tight !== 1) {
dp[idx] = dp[idx] || [];
dp[idx][sum] = dp[idx][sum] || [];
dp[idx][sum][tight] = ret;
}
return ret;
}
function rangeDigitSum(a, b) {
dp = [];
let digitA = [];
getDigits(a - 1, digitA);
let ans1 = digitSum(digitA.length - 1, 0, 1, digitA);
let digitB = [];
getDigits(b, digitB);
let ans2 = digitSum(digitB.length - 1, 0, 1, digitB);
return ans2 - ans1;
}
function main() {
let a = 123,
b = 1024;
console.log( "digit sum for given range : " + rangeDigitSum(a, b));
}
main();
|
Output:
digit sum for given range : 12613
Time Complexity:
There are total idx*sum*tight states and we are performing 0 to 9 iterations to visit every state. Therefore, The Time Complexity will be O(10*idx*sum*tight). Here, we observe that tight = 2 and idx can be max 18 for 64 bit unsigned integer and moreover, the sum will be max 9*18 ~ 200. So, overall we have 10*18*200*2 ~ 10^5 iterations which can be easily executed in 0.01 seconds.
Space Complexity:
The space complexity of this algorithm is O(d*sum*tight) as it uses a dp array of size d*sum*tight. where d is the number of digits in the number, sum is the sum of the digits and tight is a boolean value indicating whether or not the current digit is restricted to the digit in the number or not.
The above problem can also be solved using simple recursion without any memoization. The recursive solution for the above problem can be found here. We will be soon adding more problems on digit dp in our future posts.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
17 Jan, 2023
Like Article
Save Article