C++||50% beat||Solution||Time Complexity
IntuitionThe problem requires us to find two numbers with the same digit sum and maximize their sum. Since numbers with the same digit sum belong to the same gr
# Intuition
The problem requires us to find two numbers with the same digit sum and maximize their sum. Since numbers with the same digit sum belong to the same group, we can efficiently group them using a hashmap and then find the two largest numbers in each group.

# Approach

# Group numbers by digit sum

-  Compute the digit sum for each number in the input array.
- Store numbers with the same digit sum in a hashmap (unordered_map<int, vector<int>>), where the key is the digit sum, and the value is a list of numbers with that sum.

# **Find the maximum sum for each group**
- Iterate over each group in the hashmap.
- If a group has at least two numbers, sort the group and take the two largest elements.
- Keep track of the maximum sum found.
- Return the highest sum found or -1 if no valid pair exists.


# Time complexity:

- Computing the digit sum for each number takes O(log M) (where M is the maximum number in nums).
- Inserting into the hashmap takes O(1) on average.
- Sorting takes O(N log N) in total across all groups.
- Thus, the overall complexity is O(N log N).
# Space complexity:

- The hashmap stores at most O(N) numbers, leading to O(N) space in the worst case.
- The additional variables use O(1) space.
- So, the total space complexity is O(N).


# Code
```cpp []
class Solution {
public:
    int maximumSum(vector<int>& nums) {
        
        int dup=0,Sum=0;
        unordered_map<int,vector<int>>mp;
        for(int i=0;i<nums.size();i++)
        {
            Sum=0;
            dup=nums[i];
            while(dup>0)
            {
                int rem=dup%10;
                Sum=Sum+rem;
                dup=dup/10;
            }
            mp[Sum].push_back(nums[i]);

        }
        int Max_sum=-1,prev=0;
        for(auto &[key,val]:mp)
        {
            if(val.size()>=2){
            sort(val.begin(),val.end());
            prev=val[val.size()-1]+val[val.size()-2];
            Max_sum=max(Max_sum,prev);
            }

        }
        return Max_sum;
    }
};
```
['Hash Table', 'Sorting', 'Heap (Priority Queue)', 'C++']