Max Pair Sum in an Array

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.

Example 1
Inputnums = [112,131,411]
Output-1
Each number's largest digit in order is [2, 3, 4], so no valid pair exists.
Example 2
Inputnums = [2536,1613,3366,162]
Output5902
All the numbers have 6 as their largest digit, so the answer is 2536 + 3366 = 5902.

Constraints

  • 2 <= nums.length <= 100
  • 1 <= nums[i] <= 10^4

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate