Super Simple Java/C++/Python3. Solution Using Map
IntuitionApproach
Create a map to store (digSum->nums) (store nums in there corresponding digit Sums).
Find greatest and second Greatest for each digSum ( we ca
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->
1. Create a map to store (digSum->nums) (store nums in there corresponding digit Sums).
2. Find greatest and second Greatest for each digSum ( we can use PriorityQueueof size 2 also instead of List<Integer> ).
3. Sum them up to find the max.

# Complexity
- Time complexity:O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:O(n)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public int maximumSum(int[] nums) {
        Map<Integer, List<Integer>> map = new HashMap<>();
        for (int num : nums) {
            int sum = digSum(num);
            map.putIfAbsent(sum, new ArrayList<>());
            map.get(sum).add(num);
        }
        int maxSum = -1;
        for (List<Integer> arr : map.values()) {
            if (arr.size() >= 2) {
                int firstMax = 0, secondMax = 0;
                for (int x : arr) {
                    if (x > firstMax) {
                        secondMax = firstMax;
                        firstMax = x;
                    } else if (x > secondMax) {
                        secondMax = x;
                    }
                }
                maxSum = Math.max(maxSum, firstMax + secondMax);
            }
        }
        return maxSum;
    }

    private int digSum(int x) {
        int sum = 0;
        while (x != 0) {
            sum += x % 10;
            x /= 10;
        }
        return sum;
    }
}

```
```C++ []
class Solution {
public:
    int maximumSum(vector<int>& nums) {
        unordered_map<int, vector<int>> map;
        for (int num : nums) {
            int sum = digSum(num);
            map[sum].push_back(num);
        }
        int maxSum = -1;
        for (auto& [key, arr] : map) {
            if (arr.size() >= 2) {
                nth_element(arr.begin(), arr.end() - 1, arr.end());
                nth_element(arr.begin(), arr.end() - 2, arr.end());
                maxSum = max(maxSum, arr[arr.size() - 1] + arr[arr.size() - 2]);
            }
        }
        return maxSum;
    }
    int digSum(int x) {
        int sum = 0;
        while (x != 0) {
            sum += x % 10;
            x /= 10;
        }
        return sum;
    }
};
```
```Python3 []
class Solution:
    def maximumSum(self, nums: List[int]) -> int:
        digit_sum_map = defaultdict(lambda: [0, 0])
        for num in nums:
            digit_sum = self.digSum(num)
            first_max, second_max = digit_sum_map[digit_sum]
            if num > first_max:
                second_max = first_max
                first_max = num
            elif num > second_max:
                second_max = num
            digit_sum_map[digit_sum] = [first_max, second_max]

        max_sum = -1
        for first_max, second_max in digit_sum_map.values():
            if second_max > 0:
                max_sum = max(max_sum, first_max + second_max)
        return max_sum

    def digSum(self, x: int) -> int:
        return sum(int(digit) for digit in str(x))

['Hash Table', 'C++', 'Java', 'Python3']