[C++ & Java] Just Track the maximum item of each digit sum
Approach
Just keep track of the largest item for the sum of digit.
Check if the the largest before sum up with current item gives largest value or not.
Here is

# Approach

- Just keep track of the largest item for the sum of digit.
- Check if the the largest before sum up with current item gives largest value or not.


```cpp []
int maximumSum(vector<int>& nums) {
    int ans = -1;
    unordered_map<int, int> mp;
    for(int n: nums){
        int sum = 0;
        for(int t = n; t > 0; t = t/10) sum += t%10;
        if(mp[sum]) ans = max(ans, mp[sum] + n);
        mp[sum] = max(n, mp[sum]);
    }
    return ans;
}
```
```java []
int maximumSum(int[] nums) {
    int ans = -1;
    Map<Integer, Integer> map = new HashMap<>();
    for(int n: nums){
        int sum = 0;
        for(int t = n; t > 0; t = t/10) sum += t%10;
        if(map.containsKey(sum)) ans = Math.max(ans, map.get(sum) + n);
        map.put(sum, Math.max(n, map.getOrDefault(sum, 0)));
    }
    return ans;
}
```



---

<b>Here is an article of my last interview experience - A Journey to FAANG Company, I recomand you to go through this to know which all resources I have used & how I cracked interview at Amazon:
https://leetcode.com/discuss/interview-experience/3171859/Journey-to-a-FAANG-Company-Amazon-or-SDE2-(L5)-or-Bangalore-or-Oct-2022-Accepted

---


['C']