LeetCode Problem Workspace
Find Numbers with Even Number of Digits
Count the integers in an array that have an even number of digits using a direct array traversal with digit math.
2
Topics
7
Code langs
3
Related
Practice Focus
Easy · Array plus Math
Answer-first summary
Count the integers in an array that have an even number of digits using a direct array traversal with digit math.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Array plus Math
Iterate through each number in the array and compute its digit count using division or string conversion. Increment a counter whenever a number has an even number of digits. This approach leverages simple array traversal combined with math to efficiently solve the problem in linear time with constant space.
Problem Statement
Given an array of integers, your task is to determine how many numbers contain an even number of digits. For each element, count its digits and only include it if the count is even.
For example, given nums = [12,345,2,6,7896], 12 has 2 digits, 345 has 3 digits, 2 has 1 digit, 6 has 1 digit, and 7896 has 4 digits. The function should return 2 because only 12 and 7896 have an even number of digits.
Examples
Example 1
Input: nums = [12,345,2,6,7896]
Output: 2
12 contains 2 digits (even number of digits). 345 contains 3 digits (odd number of digits). 2 contains 1 digit (odd number of digits). 6 contains 1 digit (odd number of digits). 7896 contains 4 digits (even number of digits). Therefore only 12 and 7896 contain an even number of digits.
Example 2
Input: nums = [555,901,482,1771]
Output: 1
Only 1771 contains an even number of digits.
Constraints
- 1 <= nums.length <= 500
- 1 <= nums[i] <= 105
Solution Approach
Iterative Digit Counting
Loop through each number and repeatedly divide by 10 to count its digits. Increment a result counter when the digit count is even. This avoids converting numbers to strings and keeps space O(1).
String Conversion Method
Convert each number to a string and check if its length is even. This is straightforward and readable but slightly increases space usage for temporary strings.
Math Shortcut Using Logarithms
Use logarithms to determine the number of digits by calculating floor(log10(num)) + 1. Then check if the result is even to update the count, combining array iteration with a compact math computation.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | O(N) |
| Space | O(1) |
The algorithm scans each element once, giving O(N) time complexity, and uses only a counter and loop variables, maintaining O(1) space.
What Interviewers Usually Probe
- Expect you to discuss both string-based and math-based digit counting.
- They may ask how to optimize for space without creating new arrays.
- Be ready to justify O(N) time as sufficient for array sizes up to 500.
Common Pitfalls or Variants
Common pitfalls
- Forgetting to handle single-digit numbers correctly when counting digits.
- Using string conversion unnecessarily when division or log10 is faster and cleaner.
- Miscounting numbers like 10 or 100 due to off-by-one errors in digit calculation.
Follow-up variants
- Return the actual numbers with even digits instead of just the count.
- Count numbers with an odd number of digits instead of even digits.
- Apply the same logic to a list of negative numbers and zero.
FAQ
How do I efficiently find numbers with an even number of digits?
Iterate through each number, count digits using division or log10, and increment a counter whenever the digit count is even.
Can I use strings to solve Find Numbers with Even Number of Digits?
Yes, converting numbers to strings and checking the length is valid, though math-based counting is more space-efficient.
What is the time complexity of this solution?
Time complexity is O(N) because each number is visited once to compute its digits.
Are there special cases to watch out for?
Single-digit numbers, powers of ten, and the maximum allowed value 105 should be counted correctly using the chosen digit-counting method.
Is using logarithms better than string conversion for this problem?
Logarithms are more space-efficient and slightly faster, but string conversion is simpler to implement and understand.
Solution
Solution 1: Simulation
We traverse each element $x$ in the array $\textit{nums}$. For the current element $x$, we directly convert it to a string and then check if its length is even. If it is, we increment the answer by one.
class Solution:
def findNumbers(self, nums: List[int]) -> int:
return sum(len(str(x)) % 2 == 0 for x in nums)Continue Topic
array
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Array plus Math
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward