🚀 Maximum Sum of Pair with Equal Digit Sum || Easy solution
IntroductionThe problem requires us to find two indices i and j such that:
i=j
The sum of digits of nums[i] is equal to the sum of digits of nums[j]
We maximiz
# Introduction

The problem requires us to find two indices $$i$$ and $$j$$ such that:
- $$i \neq j$$
- The sum of digits of $$nums[i]$$ is equal to the sum of digits of $$nums[j]$$
- We maximize $$nums[i] + nums[j]$$

If no such indices exist, return $$-1$$.

---

# Step-by-Step Approach ✨

To solve this problem efficiently, we use a **hash map** (`unordered_map<int, priority_queue<int>>`) to group numbers by their digit sum and store the **largest values**.

### **Data Structures Used**

- `unordered_map<int, priority_queue<int>> mp;`
  - Maps the sum of digits to a **max-heap** storing numbers with that sum.
- `priority_queue<int>`
  - A max-heap that keeps track of the largest numbers with the same digit sum.

## **Operations**

#### **1. Calculate Digit Sum**
For each number in `nums`, compute the sum of its digits and store the number in the corresponding heap in `mp`.

#### **2. Find Maximum Pair Sum**
Iterate through the hash map, and for each digit sum, if more than one number exists, extract the **two largest numbers** and compute their sum.

---

# Complexity Analysis ⏳

- **Time Complexity:**
  - Calculating digit sum: **$$O(K)$$ per number** (K is the number of digits, at most 10 for `int`).
  - Storing in hash map and heap operations: **$$O(log N)$$ per number**.
  - Extracting top two elements from the heap: **$$O(1)$$**.
  - Overall complexity: **$$O(N log N)$$**.

- **Space Complexity:** **$$O(N)$$** for storing numbers in the hash map.

---

# Code 💻

```cpp []
class Solution {
public:
    int maximumSum(vector<int>& nums) {
        unordered_map<int, priority_queue<int>> mp;
        for (int i : nums) {
            int sum = 0, n = i;
            while (n) {
                sum += n % 10;
                n /= 10;
            }
            mp[sum].push(i);
        }

        int ans = -1;
        for (auto& i : mp) {
            if (i.second.size() > 1) {
                int sum = i.second.top();
                i.second.pop();
                sum += i.second.top();
                ans = max(ans, sum);
            }
        }
        return ans;
    }
};
```


```python []
class Solution:
    def maximumSum(self, nums):
        mp = defaultdict(list)
        
        for num in nums:
            digit_sum = sum(int(d) for d in str(num))
            heapq.heappush(mp[digit_sum], -num)  # Store as negative for max heap
        
        ans = -1
        for values in mp.values():
            if len(values) > 1:
                max1 = -heapq.heappop(values)
                max2 = -heapq.heappop(values)
                ans = max(ans, max1 + max2)
                
        return ans
```

---

## Example Walkthrough 📌

### **Example 1**

#### **Input:**
```cpp
nums = [18,43,36,13,7]
```

#### **Execution:**
- **Digit Sum 9** → [18, 36]
- **Digit Sum 7** → [43, 7]
- Maximum sum = `18 + 36 = 54`

#### **Output:**
```cpp
54
```

### **Example 2**

#### **Input:**
```cpp
nums = [10,12,19,14]
```

#### **Execution:**
- No two numbers share the same digit sum.

#### **Output:**
```cpp
-1
```

---

##### Thank you for your time! I hope this guide was helpful. If you found this useful, feel free to share or ask questions! 😊🚀

$$ $$
![1HBC0324_COV.jpg](https://assets.leetcode.com/users/images/3865b95f-f3c0-4c19-a5d4-5923bcdfedd4_1739319865.1360323.jpeg)


['Array', 'Hash Table', 'Sorting', 'Heap (Priority Queue)', 'Python', 'C++']