2342. Max Sum of a Pair With Equal Sum of Digits LeetCode Solution
In this guide, you will get 2342. Max Sum of a Pair With Equal Sum of Digits LeetCode Solution with the best time and space complexity. The solution to Max Sum of a Pair With Equal Sum of Digits 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
- Problem Statement
- Complexity Analysis
- Max Sum of a Pair With Equal Sum of Digits solution in C++
- Max Sum of a Pair With Equal Sum of Digits solution in Java
- Max Sum of a Pair With Equal Sum of Digits solution in Python
- Additional Resources
Problem Statement of 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. If no such pair of indices exists, return -1.
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
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2342. Max Sum of a Pair With Equal Sum of Digits LeetCode Solution in C++
class Solution {
public:
int maximumSum(vector<int>& nums) {
constexpr int kMax = 9 * 9; // 999,999,999
int ans = -1;
vector<vector<int>> count(kMax + 1);
for (const int num : nums)
count[getDigitSum(num)].push_back(num);
for (vector<int>& groupNums : count) {
if (groupNums.size() < 2)
continue;
ranges::sort(groupNums, greater<>());
ans = max(ans, groupNums[0] + groupNums[1]);
}
return ans;
}
private:
int getDigitSum(int num) {
int digitSum = 0;
while (num > 0) {
digitSum += num % 10;
num /= 10;
}
return digitSum;
}
};
/* code provided by PROGIEZ */
2342. Max Sum of a Pair With Equal Sum of Digits LeetCode Solution in Java
class Solution {
public int maximumSum(int[] nums) {
final int kMax = 9 * 9; // 999,999,999
int ans = -1;
List<Integer>[] count = new List[kMax + 1];
for (int i = 0; i <= kMax; ++i)
count[i] = new ArrayList<>();
for (final int num : nums)
count[getDigitSum(num)].add(num);
for (List<Integer> groupNums : count) {
if (groupNums.size() < 2)
continue;
Collections.sort(groupNums, Collections.reverseOrder());
ans = Math.max(ans, groupNums.get(0) + groupNums.get(1));
}
return ans;
}
private int getDigitSum(int num) {
int digitSum = 0;
while (num > 0) {
digitSum += num % 10;
num /= 10;
}
return digitSum;
}
}
// code provided by PROGIEZ
2342. Max Sum of a Pair With Equal Sum of Digits LeetCode Solution in Python
class Solution:
def maximumSum(self, nums: list[int]) -> int:
kMax = 9 * 9 # 999,999,999
ans = -1
count = [[] for _ in range(kMax + 1)]
for num in nums:
count[self._getDigitSum(num)].append(num)
for groupNums in count:
if len(groupNums) < 2:
continue
groupNums.sort(reverse=True)
ans = max(ans, groupNums[0] + groupNums[1])
return ans
def _getDigitSum(self, num: int) -> int:
return sum(int(digit) for digit in str(num))
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.