Max Sum of a Pair With Equal Sum of Digits - Problem
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. If no such pair of indices exists, return -1.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [18,43,25,61,52]
›
Output:
113
💡 Note:
Numbers 61 and 52 both have digit sum 7 (6+1=7, 5+2=7). Their sum 61+52=113 is the maximum possible.
Example 2 — No Valid Pairs
$
Input:
nums = [10,12,19,14]
›
Output:
-1
💡 Note:
Digit sums are: 1, 3, 10, 5. All different, so no pairs with equal digit sums exist.
Example 3 — Multiple Groups
$
Input:
nums = [23,32,41,14,50]
›
Output:
73
💡 Note:
Digit sum 5: 23(2+3=5), 32(3+2=5), 41(4+1=5), 14(1+4=5), 50(5+0=5). Maximum pair: 41+32=73.
Constraints
- 2 ≤ nums.length ≤ 105
- 1 ≤ nums[i] ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of positive integers
2
Process
Group by digit sum and find best pairs
3
Output
Maximum sum of valid pair or -1
Key Takeaway
🎯 Key Insight: Group numbers by digit sum and find the maximum pair within each group
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code