Skip to content

Commit adfbcfd

Browse files
authored
Created add-digit-dp-problem.java (#362)
Here's a solution to the open issue add digit dp problem
1 parent f7d739d commit adfbcfd

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed

Add-digit-dp-problem.java

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

0 commit comments

Comments
 (0)