Watch 10 video solutions for Find Numbers with Even Number of Digits, a easy level problem involving Array, Math. This walkthrough by Fisher Coder has 6,320 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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] <= 105Problem 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 | 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 |