Max Pair Sum in an Array - Problem
You are given an integer array nums. You have to find the maximum sum of a pair of numbers from nums such that the largest digit in both numbers is equal.
For example, 2373 is made up of three distinct digits: 2, 3, and 7, where 7 is the largest among them.
Return the maximum sum or -1 if no such pair exists.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [58, 18, 85, 56]
›
Output:
141
💡 Note:
Numbers with max digit 8: [58, 18, 85]. Best pair is 85 + 56 = 141. Wait, 56 has max digit 6, so we pair within max digit 8: 85 + 58 = 143. Actually, let me recalculate: 85 + 58 = 143 (both have max digit 8).
Example 2 — No Valid Pairs
$
Input:
nums = [1, 23, 456]
›
Output:
-1
💡 Note:
Max digits are 1, 3, and 6 respectively. No two numbers share the same max digit, so return -1.
Example 3 — Multiple Groups
$
Input:
nums = [12, 34, 56, 78, 19, 29]
›
Output:
107
💡 Note:
Max digits: [2,4,6,8,9,9]. Numbers with max digit 9: [19,29]. Sum: 19 + 29 = 48. All other max digits appear only once, so maximum sum is 48.
Constraints
- 2 ≤ nums.length ≤ 105
- 1 ≤ nums[i] ≤ 106
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code