---
# System prepended metadata

title: 2342. Max Sum of a Pair With Equal Sum of Digits
tags: [Leetcode, HashMap, Medium]

---

# 2342. Max Sum of a Pair With Equal Sum of Digits
###### tags: `Leetcode` `Medium` `HashMap`
Link: https://leetcode.com/problems/max-sum-of-a-pair-with-equal-sum-of-digits/description/
## 思路
用digitSum做key value存目前为止最大的有这个digitSum的数字
## Code
```java=
class Solution {
    public int maximumSum(int[] nums) {
        int res = -1;
        int[] dSum = new int[82];
        Arrays.fill(dSum, -1);
        for(int num:nums){
            int digitSum = getDigitSum(num);
            if(dSum[digitSum]!=-1){
                res = Math.max(dSum[digitSum]+num, res);
            }
            dSum[digitSum] = Math.max(dSum[digitSum], num);
        }
        return res;
    }
    public int getDigitSum(int num){
        int ans = 0;
        while(num!=0){
            ans += num%10;
            num /= 10;
        }
        return ans;
    }
}
```