Back to blog
Apr 11, 2025
3 min read

Sum of Good Numbers

Given an array of integers, return the sum of numbers that are divisible by their digit count.

Difficulty: Easy | Acceptance: 69.70% | Paid: No Topics: Array

You are given an integer array nums. A number nums[i] is considered good if it is divisible by the number of digits it has. Return the sum of all good numbers in nums.

Examples

Example 1

Input:

nums = [1,3,2,1,5,4], k = 2

Output:

12

Explanation: The good numbers are nums[1] = 3, nums[4] = 5, and nums[5] = 4 because they are strictly greater than the numbers at indices i - k and i + k.

Example 2

Input:

nums = [2,1], k = 1

Output:

2

Explanation: The only good number is nums[0] = 2 because it is strictly greater than nums[1].

Constraints

- 2 <= nums.length <= 100
- 1 <= nums[i] <= 1000
- 1 <= k <= floor(nums.length / 2)

String Conversion

Intuition Convert each number to a string to easily count its length (number of digits), then check divisibility.

Steps

  • Initialize a sum variable to 0.
  • Iterate through each number in the array.
  • Convert the number to a string and get its length.
  • If the number modulo the length is 0, add the number to the sum.
  • Return the sum.
python
class Solution:
    def sumOfGoodNumbers(self, nums: list[int]) -&gt; int:
        total = 0
        for num in nums:
            digits = len(str(num))
            if num % digits == 0:
                total += num
        return total

Complexity

  • Time: O(n * d), where n is the number of elements and d is the average number of digits (max 10 for 10⁹).
  • Space: O(d) for the string representation.
  • Notes: Very readable and concise.

Mathematical Logarithm

Intuition Use the mathematical property that the number of digits in an integer n is floor(log10(n)) + 1.

Steps

  • Initialize a sum variable to 0.
  • Iterate through each number in the array.
  • Calculate digits using floor(log10(num)) + 1.
  • If the number modulo the digit count is 0, add the number to the sum.
  • Return the sum.
python
import math

class Solution:
    def sumOfGoodNumbers(self, nums: list[int]) -&gt; int:
        total = 0
        for num in nums:
            digits = int(math.log10(num)) + 1
            if num % digits == 0:
                total += num
        return total

Complexity

  • Time: O(n).
  • Space: O(1).
  • Notes: Faster than string conversion but relies on floating point math which can be tricky near powers of 10 (though safe for 10⁹).

Iterative Digit Counting

Intuition Count digits by repeatedly dividing the number by 10 until it becomes 0.

Steps

  • Initialize a sum variable to 0.
  • Iterate through each number in the array.
  • Create a temporary copy of the number and a counter initialized to 0.
  • While the temporary number is greater than 0, divide it by 10 and increment the counter.
  • If the original number modulo the counter is 0, add the number to the sum.
  • Return the sum.
python
class Solution:
    def sumOfGoodNumbers(self, nums: list[int]) -&gt; int:
        total = 0
        for num in nums:
            temp = num
            digits = 0
            while temp &gt; 0:
                digits += 1
                temp //= 10
            if num % digits == 0:
                total += num
        return total

Complexity

  • Time: O(n * d).
  • Space: O(1).
  • Notes: Pure integer arithmetic, avoiding string allocation and floating point inaccuracies.