🚀✅ Array | Hashmap | Easiest Solution | 3 Approaches | Solution for LeetCode-2342
Approach-1 Using Hashmap and Priority Queue:-Intuition
The problem involves:

Finding pairs of numbers with the same digit sum.
Calculating the maximum sum of s
# Approach-1 Using Hashmap and Priority Queue:-

# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
- The problem involves:
  - Finding pairs of numbers with the same digit sum.
  - Calculating the maximum sum of such pairs.
# Approach
<!-- Describe your approach to solving the problem. -->
1. **Calculate Digit Sums**:
   - Implement a helper function to:
     - Compute the sum of digits of a given number.
2. **Group by Digit Sums**:
   - Use a hash map to:
     - Group numbers by their digit sum.
3. **Track Maximum Pairs**:
   - For each group with more than one number:
     - Identify the top two numbers.
     - Compute their sum.
   - Track the maximum sum among all pairs.
# Complexity
- **Time Complexity**:
  - Overall complexity:
    - $$O(n \log n)$$.

- **Space Complexity**:
  - HashMap and Priority Queues:
    - $$O(n)$$.

# Code
```cpp []
class Solution {
public:
    int sumOfDigits(int n){
        int sum = 0;
        while(n != 0){
            sum += n % 10;
            n /= 10;
        }
        return sum;
    }
    int maximumSum(vector<int>& nums) {

        unordered_map<int,priority_queue<int>> mp;
        for(int ele : nums)
            mp[sumOfDigits(ele)].push(ele);

        int maxSum = -1;

        for(auto& p : mp){
            auto maxHeap = p.second;
            if(maxHeap.size() > 1){
                int a = maxHeap.top();
                maxHeap.pop();
                int b = maxHeap.top();
                maxHeap.pop();
                maxSum = max(maxSum, a+b);
            }
        }
        return maxSum;
    }

};

```
# Approach-2 Using Hashmap Only:-

# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
- The problem involves:
  - Finding pairs of numbers with the same digit sum.
  - Calculating the maximum sum of such pairs.
# Approach
<!-- Describe your approach to solving the problem. -->
1. **Calculate Digit Sums**:
   - Implement a helper function to:
     - Compute the sum of digits of a given number.
2. **Group by Digit Sums**:
   - Use a hash map to:
     - Group numbers by their digit sum.
3. **Track Maximum Pairs**:
   - For each group with more than one number:
     - Identify and update the maximum number.
     - Compute their sum if a pair exists.
# Complexity
- **Time Complexity**:
  - Overall complexity:
    - $$O(n \log n)$$.

- **Space Complexity**:
  - HashMap:
    - $$O(n)$$.
# Code
```cpp []
class Solution {
public:
    int sumOfDigits(int n){
        int sum = 0;
        while(n != 0){
            sum += n % 10;
            n /= 10;
        }
        return sum;
    }

    int maximumSum(vector<int>& nums) {
        unordered_map<int, int> mp;
        int maxSum = -1;

        for(int ele : nums){
            int sumOfDigitsOfEle = sumOfDigits(ele);
            if(mp.count(sumOfDigitsOfEle))
                maxSum = max(maxSum, ele + mp[sumOfDigitsOfEle]);
            mp[sumOfDigits(ele)] = max(ele, mp[sumOfDigitsOfEle]);
        }
        return maxSum;
    }
};

```
# Approach-3 Using Vector as a Hashmap:-

# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
- The problem involves:
  - Finding pairs of numbers with the same digit sum.
  - Calculating the maximum sum of such pairs.
# Approach
<!-- Describe your approach to solving the problem. -->
1. **Calculate Digit Sums**:
   - Implement a helper function to:
     - Compute the sum of digits of a given number.
2. **Group by Digit Sums**:
   - Use a vector to:
     - Group numbers by their digit sum.
3. **Track Maximum Pairs**:
   - For each group with more than one number:
     - Identify and update the maximum number.
     - Compute their sum if a pair exists.
# Complexity
- **Time Complexity**:
  - Overall complexity:
    - $$O(n)$$.

- **Space Complexity**:
  - Vector:
    - $$O(1)$$.
# Code
```cpp []
class Solution {
public:
    int sumOfDigits(int n){
        int sum = 0;
        while(n != 0){
            sum += n % 10;
            n /= 10;
        }
        return sum;
    }

    int maximumSum(vector<int>& nums) {
        vector<int> sum(82, -1); // sum[sumOfDigits[ele]] = ele;
        int maxSum = -1;

        for(int ele : nums){
            int sumOfDigitsOfEle = sumOfDigits(ele);

            if(sum[sumOfDigitsOfEle] != -1)
                maxSum = max(maxSum, ele + sum[sumOfDigitsOfEle]);

            sum[sumOfDigits(ele)] = max(ele, sum[sumOfDigitsOfEle]);
        }
        return maxSum;
    }
};
```
# Please Upvote if you Like.
['Array', 'Hash Table', 'Sorting', 'Heap (Priority Queue)', 'C++']