✅✅ Using Map || Easy to understand || C++ Solution
IntuitionThe problem requires us to find the maximum sum of two numbers in the given array that have the same digit sum.To solve this efficiently, we can observ
# Intuition
The problem requires us to find the maximum sum of two numbers in the given array that have the same digit sum. 

To solve this efficiently, we can observe that:
- The sum of digits of a number remains constant regardless of the number’s order.
- If two numbers have the same digit sum, they should be considered together.
- Instead of checking every possible pair, we can store the maximum number for each digit sum and update our answer accordingly.

# Approach
1. **Helper Function (`findSum`)**:
   - This function computes the sum of digits for a given integer. 
   - It repeatedly extracts the last digit (`i % 10`), adds it to `ans`, and removes the last digit (`i /= 10`).

2. **Using an Unordered Map (`mp`)**:
   - The key represents the digit sum, and the value stores the maximum number seen so far with that sum.
   - This allows us to quickly check and update values without iterating over the entire array multiple times.

3. **Iterate Over `nums`**:
   - For each number `nums[i]`, compute its digit sum using `findSum()`.
   - If another number with the same digit sum exists in the map, update `ans` with their sum.
   - Update the map to store the maximum of the current number and the existing value.

4. **Return `ans`**:
   - If no valid pair is found, return `-1`, otherwise return the maximum sum found.

# Complexity Analysis
- **Time Complexity: `O(N log M)`**  
  - Computing the digit sum for a number `M` takes `O(log M)`.
  - Since we process each number once, the overall complexity is `O(N log M)`, where `N` is the size of `nums`.

- **Space Complexity: `O(N)`**  
  - The unordered map stores at most `N` entries in the worst case (when all numbers have distinct digit sums).
  - This results in `O(N)` space usage.

# Summary
- We efficiently pair numbers based on their digit sum using a hash map.
- Instead of checking all pairs (`O(N^2)`), we only check `O(N)` elements using the map.
- The solution leverages digit sum properties and an optimal lookup strategy to improve performance.

# Code
```cpp []
class Solution {
public:
    int findSum(int i){
        int ans=0;
        while(i){
            ans+=i%10;
            i/=10;
        }
        return ans;
    }
    int maximumSum(vector<int>& nums) {
        int n=nums.size();
        unordered_map<int,int>mp;
        int ans=-1;
        for(int i=0;i<n;i++){
            int sum=findSum(nums[i]);
            if(mp[sum]){
                ans=max(ans,nums[i]+mp[sum]);
            }
            mp[sum]=max(mp[sum],nums[i]);
        }
        return ans;
    }
};
```
# Have a Good Day 😊 UpVote?
['Array', 'Hash Table', 'C++']