All problems
1063MediumArrayHash TableSortingHeap (Priority Queue)

The Heaviest Pair With Matching Digit Totals

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2342Max Sum of a Pair With Equal Sum of Digits

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A list of positive whole numbers reads values. The digit total of a number is what its decimal digits add up to.

Return the largest amount any two values at different positions add up to, given that their digit totals match, or -1 when no two positions have matching digit totals.

Examples

Example 1

Input
values = [51, 71, 17, 42]
Output
93

The digit totals are six, eight, eight and six. The pair on six adds to ninety-three and the pair on eight to eighty-eight, so ninety-three wins.

Example 2

Input
values = [1, 2, 3]
Output
-1

The three digit totals are all different, so no pair qualifies.

Example 3

Input
values = [9, 90, 900]
Output
990

All three values have a digit total of nine, so the two largest pair up.

Constraints

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

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def maximum_sum(values: list[int]) -> int:
Java
public int maximumSum(int[] values)
September 7
Apply