EasyArrays & Strings

Find Numbers with Even Number of Digits โ€” Solution

Problem

Given an array of positive integers, count how many of them have an even number of digits. A single-digit number has 1 digit (odd), a two-digit number like 12 has 2 digits (even), and so on.

  • Input: nums = [12, 345, 2, 6, 7896]
  • Output: 2
  • Explanation: 12 has 2 digits and 7896 has 4 digits โ€” both even; the other three have odd digit counts.

Intuition

The simplest way to count digits is to convert the number to its string representation and measure the length. A number has an even number of digits when that length is divisible by 2. Since there is no smarter data structure or algorithm to exploit here, one clean linear pass is all it takes.

Solution โ€” String Length Check

For each number, convert it to a string, check whether the length is even, and accumulate the count.

  1. Iterate over every number in the array.
  2. Convert the number to its string representation.
  3. Check whether the string length is divisible by 2.
  4. Increment the count if so.
  5. Return the final count.
1def find_numbers(nums: list[int]) -> int:
2    return sum(1 for num in nums if len(str(num)) % 2 == 0)

Time: O(n ยท d), where d is the number of digits in the largest element (at most 6 for the given constraint of nums[i] โ‰ค 10โต), so effectively O(n).

Space: O(d) for the temporary string created per element โ€” effectively O(1).

Complexity Summary

ApproachTimeSpaceWhen to use
String Length CheckO(n)O(1)Always โ€” the constraint bounds digits to โ‰ค 6, making this linear in practice

Common Mistakes

  • Inverting the condition โ€” checking len % 2 != 0 returns the count of numbers with odd digit counts instead of even.
  • Wrapping in abs() unnecessarily โ€” the problem guarantees positive integers, so abs() adds noise without benefit.
  • Math division approach off-by-one โ€” counting how many times you can divide by 10 before reaching 0 gives digits โˆ’ 1, not digits (e.g. 12 // 10 = 1, one more division gives 0, so two divisions for a 2-digit number โ€” the loop must count iterations, not just the final value).
  • Assuming str(0) has 0 digits โ€” str(0) is "0" with length 1, which matters if you ever adapt this for arrays containing zero.
  • Range-checking only for the given constraints โ€” a hardcoded check like 10 <= num <= 99 or 1000 <= num <= 9999 works for nums[i] โ‰ค 10โต but silently breaks for larger inputs; the string approach generalizes automatically.

Related Problems

  • reverse-integer โ€” extracts individual digits via repeated division, the math-based alternative to string conversion
  • palindrome-number โ€” determines a property of a number's digit sequence without converting to string
  • plus-one โ€” manipulates the digit representation of an integer stored as an array
  • count-primes โ€” same pattern of iterating an array and counting elements that satisfy a numeric property
  • sqrtx โ€” integer math on a number's magnitude, similar to reasoning about the scale of a value

Ready to practice? Try it on SkillFlow

Adaptive problems, AI follow-up interviews, and a skill score that shows exactly where you need to improve.

Practice This Problem โ†’