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:
nums = [112,131,411]
Output: -1
Explanation: Each numbers largest digit in order is [2,3,4]
.
Example 2:
nums = [2536,1613,3366,162]
Output: 5902
Explanation: All the numbers have 6 as their largest digit, so the answer is 2536 + 3366 = 5902
.
Example 3:
nums = [51,71,17,24,42]
Output: 88
Explanation: Each number's largest digit in order is [5,7,7,4,4]
. So we have only two possible pairs, 71 + 17 = 88
and 24 + 42 = 66
.
Constraints:
2 <= nums.length <= 100
1 <= nums[i] <= 10^4
A brute-force approach involves iterating through each pair of numbers in the input array nums
, finding the largest digit in each number, and checking if the largest digits are equal. If they are, the sum of the pair is calculated, and the maximum sum is updated accordingly. If no such pair exists, return -1.
Given nums = [51, 71, 17, 24, 42]
import java.util.Arrays;
class Solution {
public int maxSum(int[] nums) {
int maxSum = -1;
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (findLargestDigit(nums[i]) == findLargestDigit(nums[j])) {
maxSum = Math.max(maxSum, nums[i] + nums[j]);
}
}
}
return maxSum;
}
private int findLargestDigit(int num) {
int largestDigit = 0;
String numStr = String.valueOf(num);
for (int i = 0; i < numStr.length(); i++) {
int digit = Character.getNumericValue(numStr.charAt(i));
largestDigit = Math.max(largestDigit, digit);
}
return largestDigit;
}
}
O(n^2), where n is the length of the input array nums
. This is because we iterate through each possible pair of numbers.
O(1), as we only use a constant amount of extra space.
An optimized approach involves using a hash map (or an array since digits range from 0-9) to store the maximum number encountered so far for each largest digit. Then, iterate through the array once. For each number, find its largest digit. Check if the largest digit is already present in the hash map. If it is, compute the sum of the current number and the number stored in the hash map for the same largest digit. Update maxSum
if the sum is larger than the current maxSum
. Update the hash map with the larger value between the current number and what already exists.
Given nums = [51, 71, 17, 24, 42]
maxSum
to -1 and a hash map largestDigitMap
.nums
:
nums[0] = 51
: Largest digit is 5. largestDigitMap
is empty. Store 51 in largestDigitMap
with key 5.nums[1] = 71
: Largest digit is 7. largestDigitMap
does not contain 7. Store 71 in largestDigitMap
with key 7.nums[2] = 17
: Largest digit is 7. largestDigitMap
contains 7 (value 71). Sum = 71 + 17 = 88. maxSum = 88
. Update largestDigitMap
with max(71, 17) = 71 for key 7.nums[3] = 24
: Largest digit is 4. largestDigitMap
does not contain 4. Store 24 in largestDigitMap
with key 4.nums[4] = 42
: Largest digit is 4. largestDigitMap
contains 4 (value 24). Sum = 24 + 42 = 66. maxSum = max(88, 66) = 88
. Update largestDigitMap
with max(24, 42) = 42 for key 4.maxSum = 88
.import java.util.Arrays;
class Solution {
public int maxSum(int[] nums) {
int maxSum = -1;
int[] largestDigitMap = new int[10];
Arrays.fill(largestDigitMap, -1);
for (int num : nums) {
int largestDigit = findLargestDigit(num);
if (largestDigitMap[largestDigit] != -1) {
maxSum = Math.max(maxSum, largestDigitMap[largestDigit] + num);
}
largestDigitMap[largestDigit] = Math.max(largestDigitMap[largestDigit], num);
}
return maxSum;
}
private int findLargestDigit(int num) {
int largestDigit = 0;
String numStr = String.valueOf(num);
for (int i = 0; i < numStr.length(); i++) {
int digit = Character.getNumericValue(numStr.charAt(i));
largestDigit = Math.max(largestDigit, digit);
}
return largestDigit;
}
}
O(n), where n is the length of the input array nums
. We iterate through the array only once.
O(1), since the largestDigitMap
array has a fixed size of 10, regardless of the input size.