1 Pass | O(n) | Constant Space | Example Walkthrough | 100% BEATS
💡 What We Need✅ Group numbers by their sum of digits
✅ Keep track of the previous largest number for each digit sum
✅ Maximize the sum of a valid pair❌ Return -

## **💡 What We Need**
✅ **Group numbers** by their **sum of digits**  
✅ **Keep track of the previous largest number** for each digit sum  
✅ **Maximize the sum** of a valid pair  

### **❌ Return `-1` when**  
- No digit sum group has **at least two elements**  

---

## 🧐 **Why 82 as Array Size?**  
Each number in `nums` is **≤ 10⁹**, so:  
- **Max possible digit sum** occurs when `999999999`  
- `9+9+9+9+9+9+9+9+9 = **81**`  
- We use **size 82** (`0 to 81`) for **direct indexing**  

---

## 📝 **Example Walkthrough**
🔹 **Input:** `nums = [18, 43, 36, 13, 7]`  

### **Step-by-Step Execution**

| Index | Number | 🔢 Sum of Digits | Existing Max | New Max Pair Sum | Updated Max (if changed) |
|--------|--------|----------------|--------------|-----------------|-------------------------|
| 0️⃣ | **18** | **9**  | `-1` | `-` | **18** ✅ |
| 1️⃣ | **43** | **7**  | `-1` | `-` | **43** ✅ |
| 2️⃣ | **36** | **9**  | `18` | `18+36=54` | **36** ✅ |
| 3️⃣ | **13** | **4**  | `-1` | `-` | **13** ✅ |
| 4️⃣ | **7**  | **7**  | `43` | `43+7=50` | **43** (unchanged) |

✅ **Valid Pairs:**  
- **(18, 36) → 18 + 36 = 54**  
- **(43, 7) → 43 + 7 = 50**  

🔥 **Max sum = `54`**  

🔹 **Output:** `54`  

---

## **⏳ Complexity**
✅ **Time:** `O(n)` (One pass through `nums`)  
✅ **Space:** `O(1)` (Fixed array of size `82`)  

---

## 💻 **Code Implementations**  

```cpp []
class Solution {
public:
    int maximumSum(vector<int>& nums) {
        int mp[82];  // Stores max num for each digit sum
        memset(mp, -1, sizeof(mp));
        int ans = -1;

        for (int num : nums) {
            int sumDigits = 0, temp = num;
            while (temp) sumDigits += temp % 10, temp /= 10;

            if (mp[sumDigits] != -1)
                ans = max(ans, num + mp[sumDigits]);

            mp[sumDigits] = max(mp[sumDigits], num);
        }

        return ans;
    }
};
```


```python []
class Solution:
    def maximumSum(self, nums):
        mp = [-1] * 82  # Fixed size array
        ans = -1

        for num in nums:
            digit_sum = sum(int(d) for d in str(num))

            if mp[digit_sum] != -1:
                ans = max(ans, num + mp[digit_sum])

            mp[digit_sum] = max(mp[digit_sum], num)

        return ans
```


```java []
class Solution {
    public int maximumSum(int[] nums) {
        int[] mp = new int[82];
        Arrays.fill(mp, -1);
        int ans = -1;

        for (int num : nums) {
            int sumDigits = 0, temp = num;
            while (temp > 0) {
                sumDigits += temp % 10;
                temp /= 10;
            }

            if (mp[sumDigits] != -1)
                ans = Math.max(ans, num + mp[sumDigits]);

            mp[sumDigits] = Math.max(mp[sumDigits], num);
        }
        return ans;
    }
}
```

```js []
var maximumSum = function(nums) {
    let mp = new Array(82).fill(-1);
    let ans = -1;

    for (let num of nums) {
        let sumDigits = [...String(num)].reduce((sum, d) => sum + Number(d), 0);

        if (mp[sumDigits] !== -1)
            ans = Math.max(ans, num + mp[sumDigits]);

        mp[sumDigits] = Math.max(mp[sumDigits], num);
    }

    return ans;
};
```


['Python', 'C++', 'Java', 'JavaScript']