🔥BEATS 💯 % 🎯 |✨SUPER EASY BEGINNERS 👏| JAVA | C | C++ | PYTHON| JAVASCRIPT | DART 
IntuitionThe problem requires us to find the maximum sum of two numbers from the array such that both numbers have the same digit sum.
The digit sum of a number
![image.png](https://assets.leetcode.com/users/images/b476ede9-d536-4319-bfc7-b013c8687a06_1739359543.708142.png)

---

# **Intuition**  
The problem requires us to find the maximum sum of two numbers from the array such that both numbers have the **same digit sum**.  
- The **digit sum** of a number is the sum of its digits.  
- If two numbers have the same digit sum, we should try to **pair the largest two** to get the maximum sum.  

---

# **Approach**  
1. **Use a HashMap (Dictionary) to Store the Largest Number for Each Digit Sum**  
   - Traverse through `nums` and compute the **digit sum** for each number.  
   - If a number with this digit sum already exists in the map:
     - Compute the sum of the current number and the stored number.
     - Update `res` if this sum is greater than the current maximum.  
     - Store the **maximum of the two** (keeping track of the largest number for that digit sum).  
   - If not, store the number in the map.  

2. **Iterate Through the Map to Find the Maximum Sum**  
   - The answer is the maximum sum obtained from valid pairs.  

3. **Edge Cases Considered**  
   - **No valid pairs** → Return `-1`.  
   - **Single element** → No sum possible.  
   - **Multiple numbers with unique digit sums** → No valid pairs formed.  

---

# **Complexity Analysis**  
- **Time Complexity**:  
  - **O(n)** → Single pass through `nums`, with `O(log n)` operations for digit sum.  

- **Space Complexity**:  
  - **O(n)** in the worst case (if all numbers have unique digit sums).  

---


```dart []
import 'dart:collection';
import 'dart:math';

class Solution {
  int maximumSum(List<int> nums) {
    int res = -1;
    final Map<int, int> freq = {};

    for (int i in nums) {
      int key = digitSum(i);
      if (freq.containsKey(key)) {
        int old = freq[key]!;
        res = max(res, old + i);
        freq[key] = max(old, i);
      } else {
        freq[key] = i;
      }
    }
    return res;
  }

  int digitSum(int n) {
    int sum = 0;
    while (n > 0) {
      sum += n % 10;
      n = n ~/ 10;
    }
    return sum;
  }
}
```


```java []
import java.util.*;

class Solution {
    public int maximumSum(int[] nums) {
        Map<Integer, Integer> freq = new HashMap<>();
        int res = -1;

        for (int num : nums) {
            int key = digitSum(num);
            if (freq.containsKey(key)) {
                res = Math.max(res, freq.get(key) + num);
                freq.put(key, Math.max(freq.get(key), num));
            } else {
                freq.put(key, num);
            }
        }

        return res;
    }

    private int digitSum(int n) {
        int sum = 0;
        while (n > 0) {
            sum += n % 10;
            n /= 10;
        }
        return sum;
    }
}
```


```javascript []
var maximumSum = function(nums) {
    let freq = new Map();
    let res = -1;

    for (let num of nums) {
        let key = digitSum(num);
        if (freq.has(key)) {
            res = Math.max(res, freq.get(key) + num);
            freq.set(key, Math.max(freq.get(key), num));
        } else {
            freq.set(key, num);
        }
    }

    return res;
};

function digitSum(n) {
    let sum = 0;
    while (n > 0) {
        sum += n % 10;
        n = Math.floor(n / 10);
    }
    return sum;
}
```


```python []
class Solution:
    def maximumSum(self, nums):
        freq = {}
        res = -1

        for num in nums:
            key = self.digitSum(num)
            if key in freq:
                res = max(res, freq[key] + num)
                freq[key] = max(freq[key], num)
            else:
                freq[key] = num

        return res

    def digitSum(self, n):
        return sum(int(digit) for digit in str(n))
```


```cpp []
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;

class Solution {
public:
    int maximumSum(vector<int>& nums) {
        unordered_map<int, int> freq;
        int res = -1;

        for (int num : nums) {
            int key = digitSum(num);
            if (freq.find(key) != freq.end()) {
                res = max(res, freq[key] + num);
                freq[key] = max(freq[key], num);
            } else {
                freq[key] = num;
            }
        }

        return res;
    }

private:
    int digitSum(int n) {
        int sum = 0;
        while (n > 0) {
            sum += n % 10;
            n /= 10;
        }
        return sum;
    }
};
```

---

![image.png](https://assets.leetcode.com/users/images/d81a7cee-fa73-41a2-82dc-65bf475d960c_1722267693.3902693.png) {:style='width:250px'}
['Array', 'Hash Table', 'C', 'C++', 'Java', 'Python3', 'JavaScript', 'Dart']