Max Pair Sum in an Array - Problem
Given an integer array nums, you need to find the maximum sum of a pair of numbers where both numbers share the same largest digit.
For example, in the number 2373, the digits are 2, 3, 3, and 7, where 7 is the largest digit. Two numbers can form a valid pair only if their largest digits are equal.
Goal: Return the maximum possible sum from such a pair, or -1 if no valid pair exists.
Example: For nums = [2536, 1613, 3103, 1730]
• 2536 → largest digit = 6
• 1613 → largest digit = 6
• 3103 → largest digit = 3
• 1730 → largest digit = 7
The pair (2536, 1613) both have largest digit 6, so the answer is 2536 + 1613 = 4149.
Input & Output
example_1.py — Basic Valid Pair
$
Input:
nums = [2536, 1613, 3103, 1730]
›
Output:
4149
💡 Note:
Numbers 2536 and 1613 both have largest digit 6. Their sum 2536 + 1613 = 4149 is the maximum possible.
example_2.py — Multiple Valid Pairs
$
Input:
nums = [51, 71, 17, 24]
›
Output:
88
💡 Note:
Numbers 51, 71, 17 all have largest digit 7. The maximum pair sum is 71 + 17 = 88.
example_3.py — No Valid Pairs
$
Input:
nums = [1, 2, 3, 4]
›
Output:
-1
💡 Note:
All numbers have different largest digits (1,2,3,4), so no valid pair can be formed.
Constraints
-
2 ≤
nums.length≤ 100 -
1 ≤
nums[i]≤ 104 - Each number has at least 1 digit
Visualization
Tap to expand
Understanding the Visualization
1
Assess Peak Skills
Find the largest digit (peak skill) for each number
2
Form Skill Groups
Group numbers with the same largest digit together
3
Find Best Teams
In each group with 2+ members, pair the two strongest
Key Takeaway
🎯 Key Insight: Instead of checking all possible pairs (O(n²)), we group numbers by their largest digit and only compare within each group, achieving O(n) time complexity.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code