2815. Max Pair Sum in an Array LeetCode Solution

In this guide, you will get 2815. Max Pair Sum in an Array LeetCode Solution with the best time and space complexity. The solution to Max Pair Sum in an Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Max Pair Sum in an Array solution in C++
  4. Max Pair Sum in an Array solution in Java
  5. Max Pair Sum in an Array solution in Python
  6. Additional Resources
2815. Max Pair Sum in an Array LeetCode Solution image

Problem Statement of Max Pair Sum in an Array

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:

Input: nums = [112,131,411]
Output: -1
Explanation:
Each numbers largest digit in order is [2,3,4].

Example 2:

Input: 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:

Input: 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] <= 104

See also  2023. Number of Pairs of Strings With Concatenation Equal to Target LeetCode Solution

Complexity Analysis

  • Time Complexity: O(\Sigma\log(\texttt{nums[i])})
  • Space Complexity: O(1)

2815. Max Pair Sum in an Array LeetCode Solution in C++

class Solution {
 public:
  int maxSum(vector<int>& nums) {
    int ans = 0;
    // maxNum[i] := the maximum num we met so far with the maximum digit i
    vector<int> maxNum(10);

    for (const int num : nums) {
      const int d = getMaxDigit(num);
      if (maxNum[d] > 0)
        ans = max(ans, num + maxNum[d]);
      maxNum[d] = max(maxNum[d], num);
    }

    return ans == 0 ? -1 : ans;
  }

 private:
  int getMaxDigit(int num) {
    int maxDigit = 0;
    while (num > 0) {
      maxDigit = max(maxDigit, num % 10);
      num /= 10;
    }
    return maxDigit;
  }
};
/* code provided by PROGIEZ */

2815. Max Pair Sum in an Array LeetCode Solution in Java

class Solution {
  public int maxSum(int[] nums) {
    int ans = 0;
    // maxNum[i] := the maximum num we met so far with the maximum digit i
    int[] maxNum = new int[10];

    for (final int num : nums) {
      final int d = getMaxDigit(num);
      if (maxNum[d] > 0)
        ans = Math.max(ans, num + maxNum[d]);
      maxNum[d] = Math.max(maxNum[d], num);
    }

    return ans == 0 ? -1 : ans;
  }

  private int getMaxDigit(int num) {
    int maxDigit = 0;
    while (num > 0) {
      maxDigit = Math.max(maxDigit, num % 10);
      num /= 10;
    }
    return maxDigit;
  }
}
// code provided by PROGIEZ

2815. Max Pair Sum in an Array LeetCode Solution in Python

class Solution:
  def maxSum(self, nums: list[int]) -> int:
    ans = 0
    # maxNum[i] := the maximum num we met so far with the maximum digit i
    maxNum = [0] * 10

    def getMaxDigit(num: int) -> int:
      maxDigit = 0
      while num > 0:
        maxDigit = max(maxDigit, num % 10)
        num //= 10
      return maxDigit

    for num in nums:
      d = getMaxDigit(num)
      if maxNum[d] > 0:
        ans = max(ans, num + maxNum[d])
      maxNum[d] = max(maxNum[d], num)

    return -1 if ans == 0 else ans
# code by PROGIEZ

Additional Resources

See also  3449. Maximize the Minimum Game Score LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.