Max Sum of a Pair With Equal Sum of Digits 💪✨ | | Beats 100% 🚀🚀 | | Solution By Dhandapani 🧠 🧠
IntuitionTo solve this problem, the main idea is to group the numbers by the sum of their digits. Then, within each group, find the two largest numbers and calc

# Intuition
To solve this problem, the main idea is to group the numbers by the sum of their digits. Then, within each group, find the two largest numbers and calculate their sum. The goal is to find the maximum sum from all these group sums.

# Approach
1. **Sum Calculation**: Create a helper function `getSum(int n)` to calculate the sum of digits for a given number `n`.
2. **Group by Sum of Digits**: Use a HashMap to group numbers by their sum of digits. The keys will be the sum of digits, and the values will be a max-priority queue (to keep track of the two largest numbers in each group).
3. **Track Maximum Sum**: Iterate over each group in the map. For groups with at least two numbers, calculate the sum of the two largest numbers and update the maximum sum if this sum is greater than the current maximum.

# Complexity
- **Time complexity**: 
  - **Sum Calculation**: Each call to `getSum` is $$O(\log n)$$, where `n` is the number. Since this is done for each element in the array, this is $$O(N \log M)$$ where `N` is the size of the array and `M` is the maximum value in the array.
  - **Grouping**: Inserting elements into the HashMap and PriorityQueue is $$O(N \log N)$$ in the worst case.
  - **Finding Maximum Sum**: Iterating over the map to find the maximum sum is $$O(K)$$, where `K` is the number of unique digit sums (which is typically much smaller than `N`).

  Overall, the time complexity is $$O(N \log M) + O(N \log N)$$.

- **Space complexity**:
  - Storing the HashMap and PriorityQueues will take $$O(N)$$ space.
  - The auxiliary space for storing sums and intermediate variables is $$O(1)$$.

  Therefore, the space complexity is $$O(N)$$.

# Code
```java []
class Solution {
    private int getSum(int n) {
        int sum = 0;
        while (n > 0) {
            sum += (n % 10);
            n /= 10;
        }
        return sum;
    }

    public int maximumSum(int[] nums) {
        Map<Integer, PriorityQueue<Integer>> map = new HashMap<>();
        for (int num : nums) {
            int sum = getSum(num);
            map.putIfAbsent(sum, new PriorityQueue<>(Collections.reverseOrder()));
            map.get(sum).offer(num);
        }

        long maxSum = -1;
        for (PriorityQueue<Integer> pq : map.values()) {
            if (pq.size() < 2) continue;
            long sum = pq.poll() + pq.poll();
            maxSum = Math.max(maxSum, sum);
        }
        return (int) maxSum;
    }
}
```

##### I hope this helps clarify the solution and makes it easier for you to understand and explain. If you need any further assistance or have any questions, feel free to ask!
['Java']