304 North Cardinal St.
Dorchester Center, MA 02124

# Max Sum of a Pair With Equal Sum of Digits LeetCode Solution

## Problem – Max Sum of a Pair With Equal Sum of Digits LeetCode Solution

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

Return the maximum value of `nums[i] + nums[j]` that you can obtain over all possible indices `i` and `j` that satisfy the conditions.

Example 1:

``````Input: nums = [18,43,36,13,7]
Output: 54
Explanation: 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.
``````

Example 2:

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

Constraints:

• `1 <= nums.length <= 105`
• `1 <= nums[i] <= 109`

### Max Sum of a Pair With Equal Sum of Digits LeetCode Solution in C++

``````int maximumSum(vector<int>& nums) {
int res = -1, d_n = {}; // 9 * 9
for (int n : nums) {
int d = 0;
for (int nn = n; nn; nn /= 10)
d += nn % 10;
if (d_n[d])
res = max(res, d_n[d] + n);
d_n[d] = max(d_n[d], n);
}
return res;
}
``````

### Max Sum of a Pair With Equal Sum of Digits LeetCode Solution in Java

``````class Solution {
public int maximumSum(int[] nums) {
HashMap<Integer, Integer> map = new HashMap<>();
int result = -1;

for (int item : nums) {
int key = getNumberTotal(item);

if (!map.containsKey(key))
map.put(key, item);
else {
result = Math.max(result, map.get(key) + item);
map.put(key, Math.max(map.get(key), item));
}
}

return result;
}

int getNumberTotal(int num) {
int result = 0;
while (num > 0) {
result += num % 10;
num /= 10;
}

return result;
}
}
``````

### Max Sum of a Pair With Equal Sum of Digits LeetCode Solution in Python

``````from heapq import heappush, heappop
class SizedHeap:
def __init__(self, size: int):
self.heap = []
self.size = size

def push(self, element: int):
if len(self.heap) < self.size:
heappush(self.heap, element)
elif element > self.heap:
heappop(self.heap)
heappush(self.heap, element)

def len(self) -> int:
return len(self.heap)

def sum(self) -> int:
return sum(self.heap)

class Solution:
def maximumSum(self, nums: List[int]) -> int:
n = len(nums)
sum_of_digits_max = defaultdict(lambda: SizedHeap(2))
ans = -1

for i, num in enumerate(nums):
tmp = num
s = 0
while num:
s += num % 10
num = num // 10
sum_of_digits_max[s].push(tmp)

for sum_digit in sum_of_digits_max:
if sum_of_digits_max[sum_digit].len() < 2: continue
ans = max(ans, sum_of_digits_max[sum_digit].sum())
return ans
``````
##### Max Sum of a Pair With Equal Sum of Digits LeetCode Solution Review:

In our experience, we suggest you solve this Max Sum of a Pair With Equal Sum of Digits LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Max Sum of a Pair With Equal Sum of Digits LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Max Sum of a Pair With Equal Sum of Digits LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions