[LeetCode] Sum of Integers with Maximum Digit Range

3982. Sum of Integers with Maximum Digit Range

You are given an integer array nums.

The digit range of an integer is defined as the difference between its largest digit and smallest digit.

For example, the digit range of 5724 is 7 - 2 = 5.

Return the sum of all integers in nums whose digit range is equal to the maximum digit range among all integers in the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

class Solution {
int helper(int n) {
int ma = n % 10, mi = n % 10;
while(n) {
int d = n % 10; n /= 10;
ma = max(ma, d);
mi = min(mi, d);
}
return ma - mi;
}
public:
int maxDigitRange(vector<int>& nums) {
int ma = -1, res = 0;
for(auto& n : nums) {
int now = helper(n);
if(now == ma) res += n;
else if(now > ma) {
ma = now;
res = n;
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/sum-of-integers-with-maximum-digit-range/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.