Skip to content

3982. Sum of Integers with Maximum Digit Range

Description

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.

Β 

Example 1:

Input: nums = [5724,111,350]

Output: 6074

Explanation:

i nums[i] Largest Smallest Digit Range
0 5724 7 2 5
1 111 1 1 0
2 350 5 0 5

The maximum digit range is 5. The integers with this digit range are 5724 and 350, so the answer is 5724 + 350 = 6074.

Example 2:

Input: nums = [90,900]

Output: 990

Explanation:

i nums[i] Largest Smallest Digit Range
0 90 9 0 9
1 900 9 0 9

The maximum digit range is 9. Both integers have this digit range, so the answer is 90 + 900 = 990.

Β 

Constraints:

  • 1 <= nums.length <= 100
  • 10 <= nums[i] <= 105

Solutions

Solution 1: Simulation

We traverse the array \(\textit{nums}\). For each integer \(x\), we extract its digits to find the largest digit \(b\) and the smallest digit \(a\), then compute the digit range \(r = b - a\). If \(r\) is greater than the current maximum digit range \(\textit{mx}\), we update \(\textit{mx} = r\) and reset the answer to \(x\); if \(r\) equals \(\textit{mx}\), we add \(x\) to the answer.

The time complexity is \(O(n \log M)\), and the space complexity is \(O(1)\), where \(n\) is the length of the array \(\textit{nums}\) and \(M\) is the maximum value in the array.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution:
    def maxDigitRange(self, nums: list[int]) -> int:
        ans = mx = 0
        for x in nums:
            a, b = 10, 0
            y = x
            while y:
                v = y % 10
                y //= 10
                a = min(a, v)
                b = max(b, v)
            r = b - a
            if mx < r:
                mx = r
                ans = x
            elif mx == r:
                ans += x
        return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
    public int maxDigitRange(int[] nums) {
        int ans = 0, mx = 0;
        for (int x : nums) {
            int a = 10, b = 0;
            for (int y = x; y > 0; y /= 10) {
                int v = y % 10;
                a = Math.min(a, v);
                b = Math.max(b, v);
            }
            int r = b - a;
            if (mx < r) {
                mx = r;
                ans = x;
            } else if (mx == r) {
                ans += x;
            }
        }
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
    int maxDigitRange(vector<int>& nums) {
        int ans = 0, mx = 0;
        for (int x : nums) {
            int a = 10, b = 0;
            for (int y = x; y > 0; y /= 10) {
                int v = y % 10;
                a = min(a, v);
                b = max(b, v);
            }
            int r = b - a;
            if (mx < r) {
                mx = r;
                ans = x;
            } else if (mx == r) {
                ans += x;
            }
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
func maxDigitRange(nums []int) (ans int) {
    mx := 0
    for _, x := range nums {
        a, b := 10, 0
        for y := x; y > 0; y /= 10 {
            v := y % 10
            a = min(a, v)
            b = max(b, v)
        }
        r := b - a
        if mx < r {
            mx = r
            ans = x
        } else if mx == r {
            ans += x
        }
    }
    return
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
function maxDigitRange(nums: number[]): number {
    let [ans, mx] = [0, 0];
    for (const x of nums) {
        let [a, b] = [10, 0];
        for (let y = x; y; y = (y / 10) | 0) {
            const v = y % 10;
            a = Math.min(a, v);
            b = Math.max(b, v);
        }
        const r = b - a;
        if (mx < r) {
            mx = r;
            ans = x;
        } else if (mx == r) {
            ans += x;
        }
    }
    return ans;
}

Comments