Find Numbers with Even Number of Digits (LeetCode 1295): Solution and Explanation
Given an array nums of positive integers, return how many of them have an even number of decimal digits. For example, 12 and 7896 qualify, while 345 and 6 do not.
Intuition
The task splits cleanly per number: measure how many digits it has, then ask whether that measurement is even. The answer is just how many numbers pass.
To measure digits without strings, use integer division. Dividing by 10 throws away the last digit, so 7896 becomes 789, then 78, then 7, then 0. The number of divisions it takes to reach 0 is exactly the digit count.
Once you have the count, digits % 2 == 0 decides whether the number goes into the answer. Nothing about one number affects another, so a single pass with a counter is all that is needed.
Approach: Count digits by repeated division
- Set
count = 0. - For each
numinnums, setdigits = 0. - While
num > 0, replacenumwithnum // 10and add 1 todigits. - If
digitsis even, add 1 tocount. - Return
countafter the last number.
Step-by-step example
nums = [12,345,2,6,7896]
- 12: 12 becomes 1, then 0, so it has 2 digits. Even, so count = 1.
- 345: 345 becomes 34, 3, then 0, so it has 3 digits. Odd, skip it.
- 2: one division reaches 0, so 1 digit. Odd, skip it.
- 6: also 1 digit. Odd, skip it.
- 7896: 789, 78, 7, 0 gives 4 digits. Even, so count = 2.
- All numbers are measured, so the function returns 2.
Code: Python, Java, C++
1class Solution:
2 def findNumbers(self, nums: List[int]) -> int:
3 count = 0
4 for num in nums:
5 digits = 0
6 while num > 0:
7 num //= 10
8 digits += 1
9 if digits % 2 == 0:
10 count += 1
11 return count1class Solution {
2 public int findNumbers(int[] nums) {
3 int count = 0;
4 for (int num : nums) {
5 int digits = 0;
6 while (num > 0) {
7 num /= 10;
8 digits++;
9 }
10 if (digits % 2 == 0) {
11 count++;
12 }
13 }
14 return count;
15 }
16}1class Solution {
2public:
3 int findNumbers(vector<int>& nums) {
4 int count = 0;
5 for (int num : nums) {
6 int digits = 0;
7 while (num > 0) {
8 num /= 10;
9 digits++;
10 }
11 if (digits % 2 == 0) {
12 count++;
13 }
14 }
15 return count;
16 }
17};Time and space complexity
O(n log M)
Each number needs one division per digit, which is about log10 of its value. With values up to 10^5 that is at most 6 divisions, so in practice the work is linear in n.
O(1)
Only the counter and the digit tally for the current number are stored.
Other approaches
String length (time O(n log M), space O(log M))
Convert each number to a string and check whether its length is even. It is the shortest code, but it allocates a string per number.
Range check (time O(n), space O(1))
Under the constraint nums[i] <= 10^5, a number has an even digit count exactly when it lies in [10, 99], [1000, 9999] or equals 100000. Three comparisons per number, but the solution breaks if the constraint changes.
Logarithm (time O(n), space O(1))
The digit count of a positive x is floor(log10(x)) + 1. This avoids a loop, though floating point rounding near powers of ten makes it a little risky.
Common mistakes
- Modifying the loop variable in languages where it is read-only or shared. In C# the foreach variable cannot be assigned, so copy it into a local before dividing.
- Using floating point division.
num / 10in JavaScript gives 1.2 for 12, and the loop would never reach exactly 0. UseMath.flooror integer division. - Off-by-one with log10.
Math.log10(1000)can come out as 2.9999999 on some platforms, which would report 3 digits instead of 4. - Counting numbers with an odd digit count by accident when flipping the condition. Check that 12 counts and 345 does not.
Frequently asked questions
Why does dividing by 10 count digits?
In base 10, integer division by 10 drops the rightmost digit. A d-digit number therefore reaches 0 after exactly d divisions.
Is the string approach slower?
Asymptotically it is the same, since converting a number to text also takes one step per digit. It does allocate memory for each string, but for at most 500 numbers the difference is negligible.
Do I need to worry about 0 or negative numbers?
Not here, because every value is at least 1. If 0 were allowed, the division loop would report 0 digits for it, so you would need to treat it as 1 digit.