O(N) solution using a Hashmap and value tuple
IntuitionApproachComplexity

Time complexity:
O(N)


Space complexity:
O(N)

Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
O(N)

- Space complexity:
O(N)

# Code
```csharp []
public class Solution {
    public int MaximumSum(int[] nums)
    {
         Dictionary<int, (int max, int secondMax)> dictionary = 
         new Dictionary<int, (int max, int secondMax)>();

        foreach (var num in nums)
        {
            int digitSum = SumNumber(num);
            if (dictionary.TryGetValue(digitSum, out var pair))
            {
                if (pair.max <= num)
                {
                    int temp = pair.max;
                    pair.max = num;
                    pair.secondMax = temp;
                }
                else if (pair.secondMax < num)
                {
                    pair.secondMax = num;
                }

                dictionary[digitSum] = pair;
            }
            else
            {
                dictionary.Add(digitSum, (num, -1));
            }
        }

        int maxSum = -1;
        foreach (var (key, value) in dictionary)
        {
            if (value.secondMax != -1)
            {
                maxSum = Math.Max(maxSum, value.max + value.secondMax);
            }
        }

        return maxSum;
    }

    int SumNumber(int number)
    {
        int result = 0;
        while (number != 0)
        {
            result += (number % 10);
            number /= 10;
        }

        return result;
    }
}
```
['C#']