Max Pair Sum in an Array
To find the maximum sum of a pair of numbers from the given array where the maximum digit in both numbers are equal, you can follow these steps:
- Create a dictionary where the key is the maximum digit of a number and the value is a list of numbers with that maximum digit.
- Iterate through the given array, finding the maximum digit of each number, and add the number to the appropriate list in the dictionary.
- Iterate through the values of the dictionary, finding the maximum sum of pairs in each list.
- Return the global maximum sum or -1 if no such pair exists.
Here’s the code:
|
|
This code groups the numbers by their maximum digit and then finds the maximum sum of pairs with the same maximum digit, returning the result.