Max Sum of a Pair With Equal Sum of Digits

You are given a 0-indexed array nums consisting of positive integers. You can choose two indices i and j, such that i != j, and the sum of digits of the number nums[i] is equal to that of nums[j].

Return the maximum value of nums[i] + nums[j] that you can obtain over all possible indices i and j that satisfy the conditions. If no such pair of indices exists, return -1.

Example 1
Inputnums = [18,43,36,13,7]
Output54
The valid pairs are (0, 2) with sum of digits 9 and value sum 54, and (1, 4) with sum of digits 7 and value sum 50, so the maximum is 54.
Example 2
Inputnums = [10,12,19,14]
Output-1
There are no two numbers that satisfy the conditions, so we return -1.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9

Asked at 6 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