Find Numbers with Even Number of Digits (LeetCode 1295): Solution and Explanation

Easy
Array
Math

Given an array nums of positive integers, return how many of them have an even number of decimal digits. For example, 12 and 7896 qualify, while 345 and 6 do not.

Intuition

The task splits cleanly per number: measure how many digits it has, then ask whether that measurement is even. The answer is just how many numbers pass.

To measure digits without strings, use integer division. Dividing by 10 throws away the last digit, so 7896 becomes 789, then 78, then 7, then 0. The number of divisions it takes to reach 0 is exactly the digit count.

Once you have the count, digits % 2 == 0 decides whether the number goes into the answer. Nothing about one number affects another, so a single pass with a counter is all that is needed.

Approach: Count digits by repeated division

  1. Set count = 0.
  2. For each num in nums, set digits = 0.
  3. While num > 0, replace num with num // 10 and add 1 to digits.
  4. If digits is even, add 1 to count.
  5. Return count after the last number.

Step-by-step example

nums = [12,345,2,6,7896]
  1. 12: 12 becomes 1, then 0, so it has 2 digits. Even, so count = 1.
  2. 345: 345 becomes 34, 3, then 0, so it has 3 digits. Odd, skip it.
  3. 2: one division reaches 0, so 1 digit. Odd, skip it.
  4. 6: also 1 digit. Odd, skip it.
  5. 7896: 789, 78, 7, 0 gives 4 digits. Even, so count = 2.
  6. All numbers are measured, so the function returns 2.

Code: Python, Java, C++

1class Solution:
2    def findNumbers(self, nums: List[int]) -> int:
3        count = 0
4        for num in nums:
5            digits = 0
6            while num > 0:
7                num //= 10
8                digits += 1
9            if digits % 2 == 0:
10                count += 1
11        return count
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Range check O(n)
Logarithm O(n)
Count digits by repeated division O(n log M)
String length O(n log M)
Time

O(n log M)

Each number needs one division per digit, which is about log10 of its value. With values up to 10^5 that is at most 6 divisions, so in practice the work is linear in n.

Space

O(1)

Only the counter and the digit tally for the current number are stored.

Other approaches

String length (time O(n log M), space O(log M))

Convert each number to a string and check whether its length is even. It is the shortest code, but it allocates a string per number.

Range check (time O(n), space O(1))

Under the constraint nums[i] <= 10^5, a number has an even digit count exactly when it lies in [10, 99], [1000, 9999] or equals 100000. Three comparisons per number, but the solution breaks if the constraint changes.

Logarithm (time O(n), space O(1))

The digit count of a positive x is floor(log10(x)) + 1. This avoids a loop, though floating point rounding near powers of ten makes it a little risky.

Common mistakes

  • Modifying the loop variable in languages where it is read-only or shared. In C# the foreach variable cannot be assigned, so copy it into a local before dividing.
  • Using floating point division. num / 10 in JavaScript gives 1.2 for 12, and the loop would never reach exactly 0. Use Math.floor or integer division.
  • Off-by-one with log10. Math.log10(1000) can come out as 2.9999999 on some platforms, which would report 3 digits instead of 4.
  • Counting numbers with an odd digit count by accident when flipping the condition. Check that 12 counts and 345 does not.

Frequently asked questions

Why does dividing by 10 count digits?

In base 10, integer division by 10 drops the rightmost digit. A d-digit number therefore reaches 0 after exactly d divisions.

Is the string approach slower?

Asymptotically it is the same, since converting a number to text also takes one step per digit. It does allocate memory for each string, but for at most 500 numbers the difference is negligible.

Do I need to worry about 0 or negative numbers?

Not here, because every value is at least 1. If 0 were allowed, the division loop would report 0 digits for it, so you would need to treat it as 1 digit.