1ms C++. 11ms JS. Three solutions. Beats 100.00%
ApproachThe simplest solution is to use Map to store the sum of the digits of numbers. To speed up the work, you can use an Array, and if you think a little, yo
# Approach
The simplest solution is to use `Map` to store the sum of the digits of numbers. To speed up the work, you can use an `Array`, and if you think a little, you can even get by with a `TypedArray` and improve the result by 2 times. Initially I stored a couple of numbers, but you can store only one number and store the maximum result in the variable `max`. An array can be used instead of a `Map` because the sum of digits of numbers up to $$10^9$$ is limited to `81`, i.e. `sum_of_digets(999999999) = 81`.

# Complexity
- Time complexity: $$O(nlogm)$$, where $$m$$ is max number. Effectively it will be a maximum of $$n*9$$ for this task.
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $$O(n)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code JS
TypedArray (Best)
![3.png](https://assets.leetcode.com/users/images/ae42246e-5a89-4fea-a402-40f46e109356_1739396467.5616114.png)
Array (Mid)
![2.png](https://assets.leetcode.com/users/images/b3482fde-bac9-4011-9eb6-7d28b5a649ce_1739396573.9154067.png)
Map (Slow, but still beats 100%)
![1.png](https://assets.leetcode.com/users/images/3e23287d-848f-4b6b-9d24-cb204a356898_1739396651.617124.png)

```javascript [TypedArray]
const maximumSum = (a) => {
    const m = new Uint32Array(82);
    let max = -1;
    for (const x of a) {
        let s = 0;
        let t = x;
        while (t !== 0) {
            s += t % 10;
            t = (t / 10) >> 0;
        }
        t = x + m[s];
        if (t !== x && t > max) max = t;
        if (x > m[s]) m[s] = x; 
    }
    return max;
};
```
```javascript [Array]
const maximumSum = (a) => {
    const m = new Array(82);
    for (const x of a) {
        let t = x;
        let s = 0;
        while (t !== 0) {
            s += t % 10;
            t = (t / 10) >> 0;
        }
        const p = m[s] ?? [-1, -1];
        if (p[1] === -1) m[s] = p;
        if (x > p[1]) p[0] = p[1], p[1] = x;
        else if (x > p[0]) p[0] = x;
    }
    let max = -1;
    for (const p of m)
        if (p && p[0] !== -1 && p[0] + p[1] > max)
            max = p[0] + p[1];
    return max;
};
```
```javascript [Map]
const maximumSum = (a) => {
    const m = new Map();
    for (const x of a) {
        let s = 0;
        let t = x;
        while (t !== 0) {
            s += t % 10;
            t = (t / 10) >> 0;
        }
        const p = m.get(s) ?? [-1, -1];
        if (p[1] === -1) m.set(s, p);
        if (x> p[1]) p[0] = p[1], p[1] = x;
        else if (x > p[0]) p[0] = x;        
    }
    let max = -1;
    for (const [x, y] of m.values()) {
        if (x !== -1 && x + y > max) max = x + y;
    }
    return max;
};
```
# Code C++
![4.png](https://assets.leetcode.com/users/images/14535691-8283-40f5-ab1b-6e9287374874_1739404702.7922516.png)

```cpp [C++]
class Solution {
public:
    int maximumSum(vector<int>& a) {
        int m[82];
        int max = -1;
        for (int x : a) {
            int s = 0;
            int t = x;
            for ( ; t; s += t % 10, t /= 10);
            t = x + m[s];
            if (t != x && t > max) max = t;
            if (x > m[s]) m[s] = x;
        }
        return max;
    }
};
```
# Code C
![5.png](https://assets.leetcode.com/users/images/31fdbf84-1d13-4f08-a669-dc8be57bb0eb_1739405075.750997.png)

```c [C]
int maximumSum(int* a, int size) {
    int m[82];
    int max = -1;
    for (int i = 0; i < size; ++i) {
        int s = 0;
        int t = a[i];
        for ( ; t; s += t % 10, t /= 10);
        t = a[i] + m[s];
        if (t != a[i] && t > max) max = t;
        if (a[i] > m[s]) m[s] = a[i];
    }
    return max;
}
```
['Array', 'Hash Table', 'C', 'Sorting', 'Heap (Priority Queue)', 'C++', 'TypeScript', 'JavaScript']