-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay53.java
49 lines (39 loc) · 1.6 KB
/
Day53.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
/* Max Sum of a Pair With Equal Sum of Digits with heap */
import java.util.HashMap;
import java.util.Map;
public class Day53 {
public int maximumSum(int[] nums) {
Map<Integer, Integer> sumOfDigitsMap = new HashMap<>();
int maxSum = -1;
// Helper function to calculate the sum of digits for a given number
// This function is optional as you can also calculate the sum of digits directly in the main loop
// but using a separate function can make the code more modular and readable
// For example:
// int sumOfDigits = calculateSumOfDigits(nums[i]);
for (int num : nums) {
int sumOfDigits = calculateSumOfDigits(num);
if (sumOfDigitsMap.containsKey(sumOfDigits)) {
int otherNum = sumOfDigitsMap.get(sumOfDigits);
maxSum = Math.max(maxSum, num + otherNum);
}
// Update the map with the current number and its sum of digits
sumOfDigitsMap.put(sumOfDigits, Math.max(num, sumOfDigitsMap.getOrDefault(sumOfDigits, num)));
}
return maxSum;
}
// Helper function to calculate the sum of digits for a given number
private int calculateSumOfDigits(int num) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
return sum;
}
public static void main(String[] args) {
Day53 solution = new Day53();
int[] nums = { 10, 12, 19, 14 };
int result = solution.maximumSum(nums);
System.out.println("Maximum sum: " + result); // Output: -1
}
}