You are given a 0-indexed array nums consisting of positive integers. You can choose two indices i and j, such that i != j, and the sum of digits of the number nums[i] is equal to that of nums[j].

Return the maximum value of nums[i] + nums[j] that you can obtain over all possible indices i and j that satisfy the conditions.

&nbsp;
Example 1:

Input: nums = [18,43,36,13,7]
Output: 54
Explanation: The pairs (i, j) that satisfy the conditions are:
- (0, 2), both numbers have a sum of digits equal to 9, and their sum is 18 + 36 = 54.
- (1, 4), both numbers have a sum of digits equal to 7, and their sum is 43 + 7 = 50.
So the maximum sum that we can obtain is 54.


Example 2:

Input: nums = [10,12,19,14]
Output: -1
Explanation: There are no two numbers that satisfy the conditions, so we return -1.


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 105
	1 &lt;= nums[i] &lt;= 109


class Solution:     # The plan here is to:
                    # 
                    #   • sort the elements of nums into a dict of maxheaps,
                    #     according to sum-of-digits.
                    #
                    #   • For each key, determine whether there are at least two 
                    #     elements in that key's values, and if so, compute the
                    #     product of the greatest two elements.
                    #
                    #   • return the the greatest such product as the answer.

                    # For example:
					
                    #     nums = [6,15,13,12,24,21] –> {3:[12,21], 4:[13], 6:[6,15,24]}
					
                    #     Only two keys qualify, 3 and 6, for which the greatest two elements
                    #     are 12,21 and 15,24, respectively. 12+21 = 33 and 15+24 = 39,
                    #     so the answer is 39.

    def maximumSum(self, nums: List[int]) -> int:
        d, mx = defaultdict(list), -1
        digits = lambda x: sum(map(int, list(str(x))))      # <-- sum-of-digits function
       
        for n in nums:                                      # <-- construct max-heaps
            heappush(d[digits(n)],-n)                       #     (note "-n") 

        for i in d:                                         # <-- pop the two greatest values off
            if len(d[i]) > 1:                               #     each maxheap (when possible) and
                mx= max(mx, -heappop(d[i])-heappop(d[i]))   #     compare with current max value.
                                                           
        return mx
class Solution {
    public int maximumSum(int[] nums) {
        HashMap<Integer, Integer> map = new HashMap<>();
        int result = -1;

        for (int item : nums) {
            int key = getNumberTotal(item);

            if (!map.containsKey(key))
                map.put(key, item);
            else {
                result = Math.max(result, map.get(key) + item);
                map.put(key, Math.max(map.get(key), item));
            }
        }

        return result;
    }

    int getNumberTotal(int num) {
        int result = 0;
        while (num > 0) {
            result += num % 10;
            num /= 10;
        }

        return result;
    }
}
class Solution {
public:
    int maximumSum(vector<int>& nums) {
        int ans=-1, sz=nums.size();
        unordered_map<int,int>mp;
        for(auto & i:nums){
            string s=to_string(i);
            int sum=0;
            for(auto & ch:s)
                sum+=(ch-'0');
            if(mp.count(sum))
                ans=max(ans,i+mp[sum]);
            mp[sum]=max(i,mp[sum]);
        }
        return ans;
    }
};
var maximumSum = function(nums) {
    let sums = nums.map(x => x.toString().split('').map(Number).reduce((a,b)=> a+b,0));
    let max = -1;
    let map =sums.reduce((a,b,c) => {
        a[b] ??= [];
        a[b].push(nums[c])
        return a;
    },{});
    Object.values(map).forEach(x => {
        if(x.length > 1){
            let temp = x.sort((a,b) => b-a);
            max = Math.max(max, temp[0]+temp[1]);
        }
    })
    return max;
};
Max Sum of a Pair With Equal Sum of Digits