Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 <= 1051 <= nums[i] <= 109Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[18,43,36,13,7]
[10,12,19,14]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2342: Max Sum of a Pair With Equal Sum of Digits
class Solution {
public int maximumSum(int[] nums) {
int[] d = new int[100];
int ans = -1;
for (int v : nums) {
int x = 0;
for (int y = v; y > 0; y /= 10) {
x += y % 10;
}
if (d[x] > 0) {
ans = Math.max(ans, d[x] + v);
}
d[x] = Math.max(d[x], v);
}
return ans;
}
}
// Accepted solution for LeetCode #2342: Max Sum of a Pair With Equal Sum of Digits
func maximumSum(nums []int) int {
d := [100]int{}
ans := -1
for _, v := range nums {
x := 0
for y := v; y > 0; y /= 10 {
x += y % 10
}
if d[x] > 0 {
ans = max(ans, d[x]+v)
}
d[x] = max(d[x], v)
}
return ans
}
# Accepted solution for LeetCode #2342: Max Sum of a Pair With Equal Sum of Digits
class Solution:
def maximumSum(self, nums: List[int]) -> int:
d = defaultdict(int)
ans = -1
for v in nums:
x, y = 0, v
while y:
x += y % 10
y //= 10
if x in d:
ans = max(ans, d[x] + v)
d[x] = max(d[x], v)
return ans
// Accepted solution for LeetCode #2342: Max Sum of a Pair With Equal Sum of Digits
impl Solution {
pub fn maximum_sum(nums: Vec<i32>) -> i32 {
let mut d = vec![0; 100];
let mut ans = -1;
for &v in nums.iter() {
let mut x: usize = 0;
let mut y = v;
while y > 0 {
x += (y % 10) as usize;
y /= 10;
}
if d[x] > 0 {
ans = ans.max(d[x] + v);
}
d[x] = d[x].max(v);
}
ans
}
}
// Accepted solution for LeetCode #2342: Max Sum of a Pair With Equal Sum of Digits
function maximumSum(nums: number[]): number {
const d: number[] = Array(100).fill(0);
let ans = -1;
for (const v of nums) {
let x = 0;
for (let y = v; y; y = (y / 10) | 0) {
x += y % 10;
}
if (d[x]) {
ans = Math.max(ans, d[x] + v);
}
d[x] = Math.max(d[x], v);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.