Max Sum of a Pair With Equal Sum of Digits - Problem

Imagine you're a data analyst looking for special number pairs in a dataset. You have an array of positive integers, and you need to find two different numbers that share a unique property: they have the same digit sum.

For example, the numbers 23 and 41 both have a digit sum of 5 (2+3=5, 4+1=5), making them a valid pair.

Your Goal: Find the pair with the maximum sum among all valid pairs. If no such pair exists, return -1.

Key Concepts:

  • Digit Sum: Sum of all digits in a number (e.g., 123 โ†’ 1+2+3 = 6)
  • Valid Pair: Two different array elements with equal digit sums
  • Objective: Maximize the sum of the pair values themselves

Input & Output

example_1.py โ€” Basic Case
$ Input: [18, 43, 36, 13, 7]
โ€บ Output: 54
๐Ÿ’ก Note: The pairs with equal digit sums are: (18,36) with digit sum 9, and (43,13) with digit sum 7. The maximum pair sum is 18+36=54.
example_2.py โ€” Multiple Valid Pairs
$ Input: [10, 12, 19, 14]
โ€บ Output: -1
๐Ÿ’ก Note: No two numbers have the same digit sum: 10โ†’1, 12โ†’3, 19โ†’10, 14โ†’5. Therefore, no valid pairs exist.
example_3.py โ€” Large Numbers
$ Input: [229, 398, 269, 317, 420, 464, 491, 218, 439]
โ€บ Output: 953
๐Ÿ’ก Note: Several pairs have equal digit sums. The pair (464, 491) both have digit sum 14 and give the maximum sum: 464+491=955. Wait, let me recalculate: 229(13), 398(20), 269(17), 317(11), 420(6), 464(14), 491(14), 218(11), 439(16). The pairs are (464,491) with sum 955, and (317,218) with sum 535. Maximum is 955.

Visualization

Tap to expand
๐Ÿ•ต๏ธ The Hash Table Detective MethodDetective's Process:18clue: 9Notebook Check:Clue 9:Best: 18 โœ“(first time)Next Suspect:36clue: 9Found Match!Clue 9 seen!Reward:36 + 18 = 54Final Notebook State:Clue 9 โ†’ Best: 36Clue 7 โ†’ Best: 52Max Reward: 95(52 + 43 = 95)
Understanding the Visualization
1
Meet a New Suspect
Calculate their clue value (digit sum) and check your notebook
2
Check for Match
If you've seen this clue before, you found a pair! Calculate the potential reward
3
Update Notebook
Remember the most valuable suspect for this clue type
4
Track Best Reward
Keep track of the highest reward found across all pairs
Key Takeaway
๐ŸŽฏ Key Insight: By storing only the maximum value for each digit sum, we can find valid pairs in one pass while immediately calculating the best possible reward, achieving optimal O(n) time complexity!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through array with O(log(max_value)) digit sum calculation per element

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Hash map stores at most k unique digit sums, where k โ‰ค min(n, max_digit_sum)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 109
  • The array contains positive integers only
  • Maximum digit sum is 9ร—10 = 90 for a 10-digit number
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28 Apple 22
42.3K Views
Medium-High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen