2902. Max Pair Sum in an Array
My accepted Python solution to LeetCode problem 2902, Max Pair Sum in an Array, running in 32ms.
- Difficulty: Easy
- Python
- Runtime 32ms
- Memory 17.5MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 32ms, memory 17.5MB, accepted 2026-01-01.
class Solution:
def maxSum(self, nums: List[int]) -> int:
def max_digit(n):
return max(int(d) for d in str(n))
# Group numbers by their maximum digit
groups = {}
for num in nums:
digit = max_digit(num)
if digit not in groups:
groups[digit] = []
groups[digit].append(num)
result = -1
for digit, group in groups.items():
if len(group) >= 2:
# Get two largest numbers in this group
group.sort(reverse=True)
result = max(result, group[0] + group[1])
return result