Find Numbers with Even Number of Digits - Solution & Explanation
Problem Statement
Given an array nums of integers, return how many of them contain an even number of digits.
Example 1:
Input: nums = [12,345,2,6,7896] Output: 2 Explanation: 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 Explanation: Only 1771 contains an even number of digits.
Constraints:
1 <= nums.length <= 5001 <= nums[i] <= 105
Approach Overview
Problem Overview: You receive an integer array nums. The task is simple: count how many numbers contain an even number of digits. Each number must be inspected to determine its digit length, then you increment a counter whenever the digit count is even.
The challenge is not algorithmic complexity but recognizing efficient ways to compute the number of digits in each integer. Two practical approaches exist: converting the number to a string or calculating the digit count mathematically using logarithms. Both run in linear time since every element in the array must be examined.
Approach 1: Convert to String Approach (Time: O(n), Space: O(1) extra)
This approach converts each integer to a string and checks the string length. Iterate through the array, convert num using functions like to_string(), str(), or String.valueOf(), then compute length. If length % 2 == 0, increment the counter. The key idea is that digit counting becomes trivial when numbers are represented as strings. The runtime is O(n) because you scan the array once, and the digit length is bounded by a small constant (at most 10 for typical constraints). Space complexity is effectively O(1) since the temporary string size is constant. This method is straightforward and widely used in quick implementations involving arrays.
Approach 2: Logarithmic Approach (Time: O(n), Space: O(1))
This method calculates the number of digits mathematically without converting the number to a string. For a positive integer num, the digit count is floor(log10(num)) + 1. Iterate through the array, compute the digit length using log10, and check whether the result is even. This avoids string creation and relies purely on arithmetic operations. The runtime remains O(n) because each element is processed once, and the space complexity stays O(1). This solution demonstrates understanding of numeric properties and basic math techniques.
Another equivalent mathematical approach is repeatedly dividing the number by 10 until it becomes zero, counting the iterations. That method also runs in O(n) because each number has a limited number of digits.
Recommended for interviews: Both approaches are acceptable. The string conversion method is the fastest to implement and easy to read, which interviewers often appreciate for an easy problem. The logarithmic method shows deeper understanding of number representation and mathematical reasoning. Start with the simple string solution to demonstrate clarity, then mention the logarithmic approach as an alternative optimization.
Approach 1: Convert to String Approach
This approach involves converting each number into a string to easily count the number of digits.
This C code goes through each number in the array, converts the number to a string, and checks if the length of that string is even. If it is, it increments the count of numbers with an even number of digits.
Complexity
Time Complexity: O(n), where n is the number of elements in the array.
Space Complexity: O(1), as we're using a fixed amount of extra space.
Approach 2: Logarithmic Approach
The logarithmic approach calculates the number of digits by using logarithms; specifically, the base-10 logarithm of a number is taken, and the result is incremented by 1 to get the total number of digits.
In C, we use the log10() function to calculate the number of digits. The logarithm of base 10 of any number n will give d - 1 where d is the number of digits in a number. By checking if (int)log10(nums[i]) + 1 is even, we determine if the number has an even digit count.
Complexity
Time Complexity: O(n), the array is processed once.
Space Complexity: O(1), constant space utilized.
Approach 3: Simulation
We traverse each element x in the array 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.
After the traversal is complete, we return the answer.
The time complexity is O(n times log M), and the space complexity is O(log M). Here, n is the length of the array nums, and M is the maximum value of the elements in the array nums.
Code
Python
Java
C++
Go
TypeScript
Rust
JavaScript
C#
Complexity Comparison
| Approach | Complexity |
|---|---|
| Convert to String Approach | Time Complexity: O(n), where n is the number of elements in the array. |
| Logarithmic Approach | Time Complexity: O(n), the array is processed once. |
| Simulation | — |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Convert to String | O(n) | O(1) | Best for readability and quick implementation when working with arrays |
| Logarithmic Digit Count | O(n) | O(1) | Preferred when avoiding string conversion and demonstrating mathematical reasoning |
Video Solution
LeetCode 1295: Find Numbers with Even Number of Digits - Interview Prep Ep 39 • Fisher Coder • 6,332 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Find Numbers with Even Number of Digits easy or hard?
Find Numbers with Even Number of Digits Python/Java solution
How to solve Find Numbers with Even Number of Digits in O(n)?
What is the best approach for Find Numbers with Even Number of Digits?
Is Find Numbers with Even Number of Digits asked at Google/Amazon/Meta?
What data structure is used in Find Numbers with Even Number of Digits?
What is the time complexity of Find Numbers with Even Number of Digits?
Ready to solve this problem?
Practice Find Numbers with Even Number of Digits with our built-in code editor and test cases.
Practice on FleetCode