forked from codemistic/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Add-digit-dp-problem.java
166 lines (76 loc) · 2.67 KB
/
Add-digit-dp-problem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import java.util.ArrayList;
import java.util.Arrays;
// Given two integers a and b. The task is to print
// sum of all the digits appearing in the
// integers between a and b
public class AMAN {
// Memoization for the state results
static long dp[][][] = new long[20][180][2];
// Stores the digits in x in a vector digit
static void getDigits(long x, ArrayList<Integer> digit)
{
while (x != 0) {
digit.add((int)(x % 10));
x /= 10;
}
}
// Return sum of digits from 1 to integer in
// digit vector
static long digitSum(int idx, int sum, int tight,
ArrayList<Integer> digit)
{
// base case
if (idx == -1)
return sum;
// checking if already calculated this state
if (dp[idx][sum][tight] != -1 && tight != 1)
return dp[idx][sum][tight];
long ret = 0;
// calculating range value
int k = (tight != 0) ? digit.get(idx) : 9;
for (int i = 0; i <= k; i++) {
// calculating newTight value for next state
int newTight
= (digit.get(idx) == i) ? tight : 0;
// fetching answer from next state
ret += digitSum(idx - 1, sum + i, newTight,
digit);
}
if (tight != 0)
dp[idx][sum][tight] = ret;
return ret;
}
// Returns sum of digits in numbers from a to b.
static int rangeDigitSum(int a, int b)
{
// initializing dp with -1
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;
// storing digits of a-1 in digit vector
ArrayList<Integer> digitA
= new ArrayList<Integer>();
getDigits(a - 1, digitA);
// Finding sum of digits from 1 to "a-1" which is
// passed as digitA.
long ans1
= digitSum(digitA.size() - 1, 0, 1, digitA);
// Storing digits of b in digit vector
ArrayList<Integer> digitB
= new ArrayList<Integer>();
getDigits(b, digitB);
// Finding sum of digits from 1 to "b" which is
// passed as digitB.
long ans2
= digitSum(digitB.size() - 1, 0, 1, digitB);
return (int)(ans2 - ans1);
}
// driver function to call above function
public static void main(String[] args)
{
int a = 123, b = 1024;
System.out.println("digit sum for given range : "
+ rangeDigitSum(a, b));
}
}