🚀 "Easiest + Optimal solution! 🏆 (Java | Python | C++) -> Engaging approach with thought process"
🎯 Problem IntuitionWe need to find two numbers in an array such that:
✅ Their digit sum is the same
✅ Their sum is the maximum among all such pairsSounds simple
# 🎯 Problem Intuition
We need to find two numbers in an array such that:
✅ Their digit sum is the same
✅ Their sum is the maximum among all such pairs

Sounds simple, right? But wait... 🤯 there’s a big catch! The brute force method (O(N²)) will time out due to large constraints! 🚨

I tried the brute approach and found a few test cases were not passing :')

So, let's be smart 🤓 and optimize this using a HashMap (dict in Python, unordered_map in C++) to store only the most relevant numbers! 🔥

# Optimized Approach - HashMap FTW!

The Brute force approach is a O(N**2) solution where you'd run two for loops, one from 0 and another from i + 1 (since it is given that indexes must not be the same) and keep a maxi variable and keep updating it. But in this case, it wont quite work due to the huge constraits! So we need to think of another approach.

Instead of checking every possible pair, we: 

🔹 Maintain a **HashMap** where:

**Key** → Digit sum of a number

**Value** → The maximum number found so far with that digit sum

🔹 Why only max? 
🤔 Because we only care about the largest sum! Smaller numbers are useless to us! 🚀

🔹 Update & Track max sum as we iterate through the array.Instead of checking every possible pair, we: 

# Complexity
- Time complexity: O(N) -> you're looping through the array once.

- Space complexity:   O(N) - In the worst case scenario, all numbers have different digit sums.

**🔥 Final Thoughts**
This problem is an **amazing way to build your logic** 💡 and get comfortable with HashMaps / Dicts in coding interviews! 🎯

If you liked the approach or found this super helpful, **SMASH that Upvote ❤️ & drop a comment if you have any doubts! 👇👇 I’d be happy to help! 🚀🔥**

# Code
```java []
class Solution {
    public int maximumSum(int[] nums) {
        HashMap<Integer, Integer> hash = new HashMap<>();
        int res = -1;

        for(int i : nums){
            int digSum = getDigitSum(i);

            if(hash.containsKey(digSum)){
                res = Math.max(res, i + hash.get(digSum));

                hash.put(digSum, Math.max(i, hash.get(digSum)));
            } else {
                hash.put(digSum, i);
            }

        }
        return res;
    }
    public int getDigitSum(int n){
        int res = 0;
        while(n>0){
            res += n % 10;
            n /= 10;
        }        
        return res;
    }
}
```
```python3 []
class Solution:
    def maximumSum(self, nums: List[int]) -> int:
        digit_sum_map = {}  # Store digit sum → max num
        res = -1

        def getDigSum(n):
            return sum(int(d) for d in str(n))

        for num in nums:
            digSum = getDigSum(num)

            if digSum in digit_sum_map:
                res = max(res, digit_sum_map[digSum] + num)
                digit_sum_map[digSum] = max(num, digit_sum_map[digSum]) 
            else:
                digit_sum_map[digSum] = num

        return res
```
```C++ []
#include <unordered_map>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    int maximumSum(vector<int>& nums) {
        unordered_map<int, int> hash;
        int res = -1;

        for(int num : nums){
            int digSum = getDigitSum(num);

            if(hash.count(digSum)){
                res = max(res, num + hash[digSum]);
                hash[digSum] = max(num, hash[digSum]);
            } else {
                hash[digSum] = num;
            }
        }
        return res;
    }

    int getDigitSum(int n){
        int res = 0;
        while(n > 0){
            res += n % 10;
            n /= 10;
        }        
        return res;
    }
};
```
['Array', 'Hash Table', 'C++', 'Java', 'Python3']