#2342
Max Sum of a Pair With Equal Sum of Digits
specialist · 620 · lc medium +29 · verified · 65.9% accepted · 1,406 likes · top 71%
Description
You are given a 0-indexed array nums of positive integers. Find two distinct indices i and j such that the sum of digits of nums[i] equals the sum of digits of nums[j].
Return the maximum value of nums[i] + nums[j] over all such pairs. Return -1 if no valid pair exists.
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.
Code
1
2
3