<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of <strong>positive</strong> integers. You can choose two indices <code>i</code> and <code>j</code>, such that <code>i != j</code>, and the sum of digits of the number <code>nums[i]</code> is equal to that of <code>nums[j]</code>.</p>

<p>Return <em>the <strong>maximum</strong> value of </em><code>nums[i] + nums[j]</code><em> that you can obtain over all possible indices </em><code>i</code><em> and </em><code>j</code><em> that satisfy the conditions.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [18,43,36,13,7]
<strong>Output:</strong> 54
<strong>Explanation:</strong> The pairs (i, j) that satisfy the conditions are:
- (0, 2), both numbers have a sum of digits equal to 9, and their sum is 18 + 36 = 54.
- (1, 4), both numbers have a sum of digits equal to 7, and their sum is 43 + 7 = 50.
So the maximum sum that we can obtain is 54.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [10,12,19,14]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There are no two numbers that satisfy the conditions, so we return -1.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

# 1. define a hashmap
#       key: sum of digits
#       value: a list of numbers whose sum of digits = key
# 2. define a integer 'result'
# 3. Sort the list, from smallest to largest
#   (by doing that, we ensure that hashmap.values are lists sorted in ascending order)
# 4. iterate over the list
#       calculate the sum of digits of nums[i], 'sum_digit'
#       if 'sum_digit' in hashmap:
#           1. calculate the sum of nums[i] and the largest number in hashmap[sum_digit]
#               curr_sum = nums[i] + hashmap[sum_digit][-1]
#           2. update 'result'
#               result = max(result, curr_sum)
#           3. update hashmap by adding nums[i] 
#               hashmap[sum_digit].append(nums[i])
#       else if 'sum_digit' not in hashmap:
#               hashmap[sum_digit] = nums[i]

class Solution:
    def maximumSum(self, nums: List[int]) -> int:
        result = -1
        hashmap = {}
        nums.sort()  # sort the list in ascending order
        print(nums)
        for i in range(len(nums)):
            num = nums[i]
            # calculate the sum of digit 
            sum_digit = 0
            while num > 0:
                sum_digit += num % 10
                num = num // 10
            # check sum_digit in hashmap
            if sum_digit in hashmap:
                result = max(result, nums[i]+hashmap[sum_digit][-1]) # compare and update result
                hashmap[sum_digit].append(nums[i])           # update hashmap by adding nums[i] 
            else:
                hashmap[sum_digit] = [nums[i]]
        return result


        
806