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

You are given a 0-indexed array nums consisting of positive integers. You can choose two indices i and j, such that i != j, and the sum of digits of the number nums[i] is equal to that of nums[j].

Return the **maximum value of nums[i] + nums[j] that you can obtain over all possible indices i and j that satisfy the conditions.**

  Example 1:

Input: nums = [18,43,36,13,7]
Output: 54
Explanation: The pairs (i, j) that satisfy the conditions are:
- (0, 2), both numbers have a sum of digits equal to 9, and their sum is 18 + 36 = 54.
- (1, 4), both numbers have a sum of digits equal to 7, and their sum is 43 + 7 = 50.
So the maximum sum that we can obtain is 54.

Example 2:

Input: nums = [10,12,19,14]
Output: -1
Explanation: There are no two numbers that satisfy the conditions, so we return -1.

  Constraints:

  • 1 <= nums.length <= 105

  • 1 <= nums[i] <= 109

JAVA

  • class Solution {
        public int maximumSum(int[] nums) {
            Map<Integer, Integer> map = new HashMap<>();
            int res = -1;
            for (int num : nums) {
                int s = 0;
                for (char digit : String.valueOf(num).toCharArray()) {
                    s += Integer.valueOf(digit - '0');
                }
                if (!map.containsKey(s)) {
                    map.put(s, num);
                } else {
                    res = Math.max(res, map.get(s) + num);
                    map.put(s, Math.max(map.get(s), num));
                }
            }
            return res;
        }
    }
    
    ############
    
    class Solution {
        public int maximumSum(int[] nums) {
            int ans = -1;
            int[] d = new int[100];
            for (int v : nums) {
                int y = 0;
                for (int x = v; x > 0; x /= 10) {
                    y += x % 10;
                }
                if (d[y] > 0) {
                    ans = Math.max(ans, d[y] + v);
                }
                d[y] = Math.max(d[y], v);
            }
            return ans;
        }
    }

C++

  • class Solution {
    public:
        int maximumSum(vector<int>& nums) {
            vector<vector<int>> d(100);
            for (int& v : nums) {
                int y = 0;
                for (int x = v; x > 0; x /= 10) {
                    y += x % 10;
                }
                d[y].emplace_back(v);
            }
            int ans = -1;
            for (auto& vs : d) {
                if (vs.size() > 1) {
                    sort(vs.rbegin(), vs.rend());
                    ans = max(ans, vs[0] + vs[1]);
                }
            }
            return ans;
        }
    };

PYTHON

  • from collections import defaultdict
    
    class Solution:
        def maximumSum(self, nums):
            digit_sums = defaultdict(list)
    
            # Group numbers by their digit sum
            for num in nums:
                digit_sum = sum(int(d) for d in str(num))
                digit_sums[digit_sum].append(num)
    
            max_sum = -1
    
            # Find the largest sum of two numbers with the same digit sum
            for group in digit_sums.values():
                if len(group) > 1:
                    group.sort(reverse=True)  # Sort descending
                    max_sum = max(max_sum, group[0] + group[1])  # Take the two largest numbers
    
            return max_sum

Comments