Input: nums =[12,345,2,6,7896]Output: 2Explanation:
12 contains 2digits(even number of digits).345 contains 3digits(odd number of digits).2 contains 1digit(odd number of digits).6 contains 1digit(odd number of digits).7896 contains 4digits(even number of digits).Therefore only 12 and 7896 contain an even number of digits.
Example 2:
1
2
3
4
Input: nums =[555,901,482,1771]Output: 1Explanation:
Only 1771 contains an even number of digits.
classSolution {
publicintfindNumbers(int[] nums) {
int ans = 0;
for (int n : nums) {
int digitCount = 0;
while (n > 0) {
n /= 10; // Reduce the number by dividing it by 10 digitCount++; // Increase digit count for every division }
if (digitCount % 2 == 0) { // Check if the digit count is even ans++;
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution:
deffindNumbers(self, nums: List[int]) -> int:
ans: int =0for n in nums:
digit_count =0while n >0:
n //=10# Reduce the number by dividing it by 10 digit_count +=1# Increase digit count for every divisionif digit_count %2==0: # Check if the digit count is even ans +=1return ans
⏰ Time complexity: O(n*d). Each division reduces the number by a factor of 10, so the number of divisions is proportional to the number of digits d. For an array of size n, it’s O(n * d) where d is the average number of digits.
To determine the number of digits in a number, convert it to a string and check its length, as this is straightforward and avoids mathematical complexity.
Check if the length of the string representation of each number is even.
Increment the count whenever a number satisfies the condition.
⏰ Time complexity: O(n*d), where n is the length of the array and d is the number of digits in the number. For each number, it takes O(d) time to count, hence O(n*d) in total.
🧺 Space complexity: O(1), as we are not using additional space proportional to the input size, aside from the space required for the count and temporary variables.
When working with numbers, finding their digit count can be efficiently determined using logarithm properties. For a positive integer n, the number of digits is calculated as:
$$
\text{digits} = \lfloor \log_{10}(n) \rfloor + 1 )
$$
This formula works because logarithm base 10 gives the exponent required to express n as 10^k.
Using this, we can:
For each number in the array, calculate the digit count using logarithms.
Check if the digit count is even.
Increment the count when the condition is satisfied.
classSolution {
publicintfindNumbers(int[] nums) {
int ans = 0;
for (int n : nums) {
// Calculate number of digits via logarithmif (n > 0 && ((int) Math.log10(n) + 1) % 2 == 0) {
ans++;
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
classSolution:
deffindNumbers(self, nums: List[int]) -> int:
ans: int =0for n in nums:
# Calculate number of digits via logarithmif n >0and (int(math.log10(n)) +1) %2==0:
ans +=1return ans
⏰ Time complexity: O(n), where n is the length of the array. Each number is processed individually in O(1) time.
🧺 Space complexity: O(1), as we are not using additional space proportional to the input size, aside from the space required for the count and temporary variables.