Beats 100% | 🔥 Efficient Grouping Approach 💡 | Easiest Solution Ever | Algorithm Explained
Youtube🚀 Beats 100% | 🔥 Efficient Grouping Approach 💡 | Easiest Solution Ever | Algorithm Explained🔼 Please Upvote🔼 Please Upvote🔼 Please Upvote🔼 Please Upvote💡
# Youtube
https://youtu.be/WDBb_E5N0YU

![max.png](https://assets.leetcode.com/users/images/2a2a946c-6e92-400b-93c3-093406f478e5_1739332017.4449005.png)



---

# 🚀 **Beats 100% | 🔥 Efficient Grouping Approach 💡 | Easiest Solution Ever | Algorithm Explained**  

# **🔼 Please Upvote**  
# **🔼 Please Upvote**  
# **🔼 Please Upvote**  
# **🔼 Please Upvote**  

---

## **💡 Problem Breakdown**  
Given an array `nums` of positive integers, we need to find the maximum sum of two numbers such that the **sum of their digits is equal**. If no such pair exists, return `-1`.  

---

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

#### **Output:**  
```plaintext
54
```

#### **Explanation:**  
| Step | Number | Sum of Digits | Max Value for This Sum Before | New Max Sum |
|------|--------|---------------|--------------------------------|-------------|
| 1️⃣  | 18     | 9             | 0                              | 18          |
| 2️⃣  | 43     | 7             | 0                              | 43          |
| 3️⃣  | 36     | 9             | 18                             | 54          |
| 4️⃣  | 13     | 4             | 0                              | 13          |
| 5️⃣  | 7      | 7             | 43                             | (no update) |

✅ **Final Result:** `54`  

---

## **📝 Game Plan**  

1️⃣ **Sum of Digits Calculation:**  
   - For each number, calculate its sum of digits using division and modulo operations.  

2️⃣ **Track Maximum Values:**  
   - Use an array of size 82 to store the largest number for each possible digit sum.  
   - If a second number with the same digit sum is found, calculate their sum and update the maximum result if needed.  

3️⃣ **Efficient Updates:**  
   - Keep the maximum number for each sum of digits to avoid redundant calculations.  

---

## **🚀 Code Implementations**  

### **🟢 Java Solution**
```java
class Solution {
    public int maximumSum(int[] nums) {
        int[] max = new int[82];
        int ans = -1;
        for (int x : nums) {
            int sum = 0;
            int temp = x;
            while (temp != 0) {
                sum += temp % 10;
                temp /= 10;
            }
            if (max[sum] != 0) {
                ans = Math.max(ans, x + max[sum]);
            }
            max[sum] = Math.max(max[sum], x);
        }
        return ans;
    }
}
```

### **🟣 C++ Solution**
```cpp
class Solution {
public:
    int maximumSum(vector<int>& nums) {
        int max[82] = {0};
        int ans = -1;
        for (int x : nums) {
            int sum = 0, temp = x;
            while (temp != 0) {
                sum += temp % 10;
                temp /= 10;
            }
            if (max[sum] != 0) ans = std::max(ans, x + max[sum]);
            max[sum] = std::max(max[sum], x);
        }
        return ans;
    }
};
```

### **🟡 Python 3 Solution**
```python
class Solution:
    def maximumSum(self, nums: List[int]) -> int:
        max_arr = [0] * 82
        ans = -1
        for x in nums:
            digit_sum = sum(int(d) for d in str(x))
            if max_arr[digit_sum] != 0:
                ans = max(ans, x + max_arr[digit_sum])
            max_arr[digit_sum] = max(max_arr[digit_sum], x)
        return ans
```

### **🟠 C Solution**
```c
int maximumSum(int* nums, int numsSize) {
    int max[82] = {0};
    int ans = -1;
    for (int i = 0; i < numsSize; i++) {
        int sum = 0, temp = nums[i];
        while (temp != 0) {
            sum += temp % 10;
            temp /= 10;
        }
        if (max[sum] != 0) ans = (ans > nums[i] + max[sum]) ? ans : nums[i] + max[sum];
        max[sum] = (max[sum] > nums[i]) ? max[sum] : nums[i];
    }
    return ans;
}
```

### **🔵 JavaScript Solution**
```javascript
var maximumSum = function(nums) {
    let max = new Array(82).fill(0);
    let ans = -1;
    for (let x of nums) {
        let sum = 0, temp = x;
        while (temp !== 0) {
            sum += temp % 10;
            temp = Math.floor(temp / 10);
        }
        if (max[sum] !== 0) ans = Math.max(ans, x + max[sum]);
        max[sum] = Math.max(max[sum], x);
    }
    return ans;
};
```

---

## **📊 Complexity Analysis**  
- **Time Complexity:** $$O(n log m)$$ → We process each number once, and calculating the sum of digits for a number takes $$O(log m)$$, where $$m$$ is the largest number and $$n$$ is the size of the input array.  
- **Space Complexity:** **O(1)** → The array used to store maximum values has a constant size of 82.  

---

## **💡 Why This Works Like Magic?**  
✔️ **Efficient and Optimal:** No need to check every possible pair of numbers.  
✔️ **Constant Space:** The space usage is minimal and doesn’t grow with input size.  
✔️ **Handles Edge Cases Gracefully:** Works correctly for small arrays, duplicates, and single-element cases.  

---

# **🔼 Please Upvote**  
# **🔼 Please Upvote**  
# **🔼 Please Upvote**  
# **🔼 Please Upvote**  

💡 **If this helped, an upvote would be awesome! 🚀🔥**  
['Array', 'Hash Table', 'C', 'Sorting', 'Heap (Priority Queue)', 'C++', 'Java', 'Python3', 'JavaScript']