Simple Solution (2 approach)
Complexity
Time complexity: O(n^2 * log(max_num))

Space complexity: O(1)
Code(Giving TLE)Optimized CodeComplexity
Time complexity: O(N∗Log(N))

Space complexit
# Complexity
- Time complexity: O(n^2 * log(max_num))
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

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

# Code(Giving TLE)
```cpp []
class Solution {
public:
    int digitSum(int num){
        int sum = 0;
        while(num){
            sum += num%10;
            num /= 10;
        }
        return sum;
    }
    int maximumSum(vector<int>& nums) {
        int maxSum = -1;
        for(int i = 0; i<nums.size(); i++){
            int sum = 0;
            for(int j = i+1; j<nums.size(); j++){
                if(digitSum(nums[i]) == digitSum(nums[j])){
                    sum = nums[i]+nums[j];
                    maxSum = max(sum, maxSum);
                }
            }
        }
        return maxSum;
    }
};
```
# Optimized Code
# Complexity
- Time complexity: O(N∗Log(N))
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(N)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
```cpp []
class Solution {
public:
    int digitSum(int num){
        int sum = 0;
        while(num){
            sum += num%10;
            num /= 10;
        }
        return sum;
    }
    int maximumSum(vector<int>& nums) {
        int maxSum = -1;
        unordered_map<int, vector<int>> mp;
        for(int num:nums){
            int sum = digitSum(num);
            mp[sum].push_back(num);
        }
        for(auto& m:mp){
            if(m.second.size() >= 2){
                sort(m.second.rbegin(), m.second.rend());
                int sum = m.second[0] + m.second[1];
                maxSum = max(sum, maxSum);
            }
        }
        return maxSum;
    }
};
```
['Array', 'Hash Table', 'Sorting', 'C++']