C++ Solution using a HashMap, O(n) Time Complexity and O(n) Space Complexity
Approach
My thought process for example was to create a hashmap of int, int. Where the key was the sum of an individual number, and the value is the max of that
# Approach
- My thought process for example was to create a hashmap of int, int. Where the key was the sum of an individual number, and the value is the max of that sum.
- For example, 18 has a sum of 1 + 8 which is 9, and 36 has a sum of 3 + 6 which is also 9.
- In the hashmap, we want to add the key which is 9, and the value of whichever is bigger
- So our map with, with an example nums arr: [18, 43, 36, 12, 89] would be: {9: 18} on the first iteration of the code
- On the third iteration, we see 36's individual sum also adds up to 9
- But since both 36 and 18 have both key 9, we can just do whichever is greater by doing the max between each (max(18, 36)) == 36.
- Below is the following approach:

# Complexity
- Time complexity: O(n)

- Space complexity: O(n)

# Code
```cpp []
class Solution {
private:
    int calcIndivSum(int num) {
        int res = 0;
        while (num > 0) {
            res += num % 10;
            num /= 10;
        }

        return res;
    }

public:
    int maximumSum(vector<int>& nums) {
        int maxNum = -1;
        unordered_map<int, int> digitSum;

        for (const int &num : nums) {
            int indivSum = calcIndivSum(num);

            // if it is in the map:
            if (digitSum.find(indivSum) != digitSum.end()) {
                maxNum = max(maxNum, num + digitSum[indivSum]);
                digitSum[indivSum] = max(digitSum[indivSum], num);

            // otherwise, just append the key as indivSum, and value is the number itself
            } else {
                digitSum[indivSum] = num;

            }
        }

        return maxNum;
    }
};
```
['Hash Table', 'C++']