LeetCode solutions

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

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 32ms, memory 17.5MB, accepted 2026-01-01.

python
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

Source