Difficulty: Easy | Acceptance: 79.70% | Paid: No Topics: Array, Math
Given an array nums of integers, return how many of them contain an even number of digits.
- Examples
- Constraints
- Approach 1: String Conversion
- Approach 2: Mathematical Division
- Approach 3: Logarithmic Approach
Examples
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 <= 500
1 <= nums[i] <= 10^5
Approach 1: String Conversion
Intuition Convert each integer to a string and simply check the length of that string. If the length is divisible by 2, the digit count is even.
Steps
- Initialize a counter to 0.
- Iterate through each number in the array.
- Convert the number to a string.
- Check if the length of the string modulo 2 is 0.
- If yes, increment the counter.
- Return the counter.
python
class Solution:
def findNumbers(self, nums: list[int]) -> int:
count = 0
for num in nums:
if len(str(num)) % 2 == 0:
count += 1
return countComplexity
- Time: O(N * K), where N is the number of elements and K is the average number of digits. Since K is bounded by a constant (max 6 digits for 10⁵), this is effectively O(N).
- Space: O(N) or O(1) depending on implementation details (creating strings requires auxiliary space).
- Notes: This is often the most readable solution, though slightly less performant than pure math due to string allocation overhead.
Approach 2: Mathematical Division
Intuition Count the digits of each number by repeatedly dividing it by 10 until it becomes 0. Each division operation effectively strips off the last digit.
Steps
- Initialize a counter to 0.
- Iterate through each number in the array.
- Initialize a digit count to 0.
- Use a temporary variable to hold the current number.
- While the temporary number is greater than 0, divide it by 10 and increment the digit count.
- Check if the digit count is even.
- If yes, increment the main counter.
- Return the main counter.
python
class Solution:
def findNumbers(self, nums: list[int]) -> int:
count = 0
for num in nums:
digits = 0
temp = num
while temp > 0:
digits += 1
temp //= 10
if digits % 2 == 0:
count += 1
return countComplexity
- Time: O(N * K), where K is the number of digits. Since K is small, it is effectively O(N).
- Space: O(1), as we only use a few integer variables for counting.
- Notes: This approach avoids the overhead of string allocation and is very efficient in terms of memory.
Approach 3: Logarithmic Approach
Intuition The number of digits D in a positive integer N can be calculated using the formula D = floor(log10(N)) + 1. We can use this mathematical property to determine the digit count instantly.
Steps
- Initialize a counter to 0.
- Iterate through each number in the array.
- Calculate the number of digits using the logarithm formula.
- Check if the result is even.
- If yes, increment the counter.
- Return the counter.
python
import math
class Solution:
def findNumbers(self, nums: list[int]) -> int:
count = 0
for num in nums:
# log10 returns float, floor gives int part, +1 for total digits
if (int(math.log10(num)) + 1) % 2 == 0:
count += 1
return countComplexity
- Time: O(N), as calculating the logarithm is considered an O(1) operation for fixed-size integers.
- Space: O(1).
- Notes: This is a constant-time operation per number, but floating-point operations can sometimes be slower than integer division loops for small numbers due to CPU architecture specifics.