easy priority_queue + hashmap solution c++
IntuitionThe problem requires us to find the maximum sum of two numbers in the given array that have the same sum of digits.
Observation:

The sum of digits of
# Intuition
The problem requires us to find the maximum sum of two numbers in the given array that have the same sum of digits.

1) Observation:

* The sum of digits of a number is a key property that can group numbers together.
* If multiple numbers share the same sum of digits, the two largest among them can form a valid pair.
2) Idea:

* Use a hashmap (unordered_map/map) where the key is the sum of digits of a number, and the value is a max-heap (priority_queue) storing numbers with that digit sum.
* This allows efficient retrieval of the two largest numbers for each digit sum category.

# Approach
1) Calculate the digit sum for each number:

* Iterate over nums[], compute the sum of its digits, and store the number in a map (mp), where:
* Key = Sum of digits.
* Value = A max-heap (priority_queue<int>) storing numbers.
2) Find the two largest numbers for each digit sum:

* Iterate through the map:
* If there are at least two numbers in the priority queue, extract the top two elements and compute their sum.
* Keep track of the maximum sum encountered.
3) Return the maximum sum found, or -1 if no valid pair exists.


# Complexity
- Time complexity:
O(N)
- Space complexity:
O(N)
# Code
```cpp []
#include <bits/stdc++.h>
using namespace std;

class Solution {
    int findSum(int i) {
        int sum = 0;
        while (i) {
            sum += i % 10;
            i = i / 10;
        }
        return sum;
    }

public:
    int maximumSum(vector<int>& nums) {
        int ans = -1;
        map<int, priority_queue<int>> mp;

        // Store numbers in a priority queue based on their digit sum
        for (int num : nums) {
            int sumDigits = findSum(num);
            mp[sumDigits].push(num);
        }

        // Iterate over map and find the max sum of two numbers
        for (auto& it : mp) {
            if (it.second.size() < 2) continue;  // Skip if fewer than 2 numbers

            int n1 = it.second.top();
            it.second.pop();
            int n2 = it.second.top();
            
            ans = max(ans, n1 + n2);
        }
        return ans;
    }
};



```
['C++']