Ctrl K

Best Pair With Matching Digit Sums

Medium
You are given an array of positive integers values. Two elements at different positions make a pair if their digit sums are equal (for example, 15 and 42 both have digit sum 6). Return the largest sum of two numbers that make a pair. If there is no pair at all, return -1.
Example 1
values=[24, 51, 15, 60, 33] → 111

24, 51, 15, 60 and 33 all have digit sum 6. The two largest are 60 and 51, giving 111.

Example 2
values=[7, 70, 12, 3] → 77

7 and 70 have digit sum 7; 12 and 3 have digit sum 3. The best pair is 7 + 70 = 77.

Example 3
values=[5, 14, 22] → 19

The digit sums are 5, 5 and 4, so the pair 5 and 14 gives 19.

Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • 1 ≤ values[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMost Common Values MediumJobs With a Cooldown MediumMost Searched Words MediumSpread Out the Labels MediumCross Out Half the List Medium
loading Python…
Input
values=[24, 51, 15, 60, 33]
Expected
111

Run checks the first 3 tests in your browser. Submit checks all 118 tests on the server, hidden ones included.