Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1295. Find Numbers with Even Number of Digits - Leetcode Solution

Problem Description

Given an array of integers nums, your task is to count how many numbers in the array contain an even number of digits.

For example, given nums = [12, 345, 2, 6, 7896], the numbers with an even number of digits are 12 and 7896 (since they have 2 and 4 digits, respectively).

Constraints:

  • 1 <= nums.length <= 500
  • 1 <= nums[i] <= 10^5
The solution should return a single integer: the count of numbers in nums that have an even number of digits.

Thought Process

The problem asks us to count numbers in an array based on the number of digits they have. The most direct way is to check each number, determine how many digits it contains, and count it if that number is even.

At first, you might think to convert each number to a string and check the length, since counting characters in a string is straightforward. Alternatively, you could use math: repeatedly divide the number by 10 until it becomes 0, counting how many times this happens.

Both approaches are valid. Since the constraints are small (arrays up to 500 elements), either approach will work efficiently. However, converting to a string is typically simpler and more readable.

We want to avoid unnecessary complexity, so we look for the most direct and efficient solution that fits the problem's constraints.

Solution Approach

Let's break down the solution step by step:

  1. Iterate through each number in nums:
    • We need to examine every element, so we use a loop.
  2. Count the number of digits in the current number:
    • String Approach: Convert the number to a string and use its length.
    • Math Approach: Use a loop to divide the number by 10 until it becomes 0, incrementing a digit counter each time.
  3. Check if the digit count is even:
    • If it is, increment a result counter.
  4. Return the final count:
    • This is the answer.

Design Choices:
  • We use the string approach for clarity, but both methods are efficient given the constraints.
  • No extra data structures are needed, so space usage is minimal.

Example Walkthrough

Let's use the sample input nums = [12, 345, 2, 6, 7896]:

  1. 12: As a string, "12" has 2 digits (even) → count = 1
  2. 345: "345" has 3 digits (odd) → count remains 1
  3. 2: "2" has 1 digit (odd) → count remains 1
  4. 6: "6" has 1 digit (odd) → count remains 1
  5. 7896: "7896" has 4 digits (even) → count = 2

Final answer: 2 numbers have an even number of digits.

Time and Space Complexity

Brute-force Approach:

  • For each number, we convert it to a string and check its length. This takes O(1) time per number (since numbers are at most 5 digits).
  • For n numbers, the total time is O(n).
  • Space complexity is O(1), since we only use a counter variable.
Optimized Approach:
  • Using division instead of string conversion does not change the time complexity, since the number of digits is at most 5.
  • Overall, both approaches are O(n) time and O(1) space.

Summary

The problem reduces to checking, for each number in the array, whether its digit count is even. We can efficiently do this by converting each number to a string and checking the length, or by counting digits mathematically. The solution is straightforward and efficient due to the small input size, and it highlights the value of simple, readable code for common array-processing tasks.

Code Implementation

class Solution:
    def findNumbers(self, nums):
        count = 0
        for num in nums:
            if len(str(num)) % 2 == 0:
                count += 1
        return count
      
class Solution {
public:
    int findNumbers(vector<int>& nums) {
        int count = 0;
        for (int num : nums) {
            int digits = 0, n = num;
            while (n > 0) {
                digits++;
                n /= 10;
            }
            if (digits % 2 == 0) {
                count++;
            }
        }
        return count;
    }
};
      
class Solution {
    public int findNumbers(int[] nums) {
        int count = 0;
        for (int num : nums) {
            int digits = 0, n = num;
            while (n > 0) {
                digits++;
                n /= 10;
            }
            if (digits % 2 == 0) {
                count++;
            }
        }
        return count;
    }
}
      
var findNumbers = function(nums) {
    let count = 0;
    for (let num of nums) {
        if (num.toString().length % 2 === 0) {
            count++;
        }
    }
    return count;
};