Skip to content

Commit 2daac9d

Browse files
authored
Update Max Sum of a Pair With Equal Sum of Digits.java
1 parent 583c210 commit 2daac9d

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
class Solution {
2-
public int maximumSum(int[] nums) {
3-
int maxSum = -1;
4-
Map<Integer, Integer> map = new HashMap<>();
5-
for (int num : nums) {
6-
int digitSum = getDigitSum(num);
7-
if (map.containsKey(digitSum)) {
8-
maxSum = Math.max(maxSum, map.get(digitSum) + num);
9-
map.put(digitSum, Math.max(map.get(digitSum), num));
10-
} else {
11-
map.put(digitSum, num);
12-
}
2+
public int maximumSum(int[] nums) {
3+
Map<Integer, Integer> map = new HashMap<>();
4+
int maxSum = -1;
5+
for (int num : nums) {
6+
int digitSum = calculateDigitSum(num);
7+
if (map.containsKey(digitSum)) {
8+
maxSum = Math.max(maxSum, map.get(digitSum) + num);
9+
map.put(digitSum, Math.max(map.get(digitSum), num));
10+
} else {
11+
map.put(digitSum, num);
12+
}
13+
}
14+
return maxSum;
1315
}
14-
return maxSum;
15-
}
16-
17-
private int getDigitSum(int num) {
18-
int sum = 0;
19-
while (num > 0) {
20-
sum += num % 10;
21-
num /= 10;
16+
17+
private int calculateDigitSum(int num) {
18+
int sum = 0;
19+
while (num > 0) {
20+
sum += num % 10;
21+
num /= 10;
22+
}
23+
return sum;
2224
}
23-
return sum;
24-
}
2525
}

0 commit comments

Comments
 (0)