Skip to main content

Find Numbers with Even Number of Digits - Solution & Explanation

EasyArrayMath13 min readAsked at: Amazon, Microsoft, Meta +4
Practice this problem

Problem Statement

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 <= 500
  • 1 <= nums[i] <= 105

Approach Overview

Problem 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 1: Convert to String Approach

This approach involves converting each number into a string to easily count the number of digits.

This C code goes through each number in the array, converts the number to a string, and checks if the length of that string is even. If it is, it increments the count of numbers with an even number of digits.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the number of elements in the array.
Space Complexity: O(1), as we're using a fixed amount of extra space.

Try this approach in the editor →

Approach 2: Logarithmic Approach

The logarithmic approach calculates the number of digits by using logarithms; specifically, the base-10 logarithm of a number is taken, and the result is incremented by 1 to get the total number of digits.

In C, we use the log10() function to calculate the number of digits. The logarithm of base 10 of any number n will give d - 1 where d is the number of digits in a number. By checking if (int)log10(nums[i]) + 1 is even, we determine if the number has an even digit count.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), the array is processed once.
Space Complexity: O(1), constant space utilized.

Try this approach in the editor →

Approach 3: Simulation

We traverse each element x in the array nums. For the current element x, we directly convert it to a string and then check if its length is even. If it is, we increment the answer by one.

After the traversal is complete, we return the answer.

The time complexity is O(n times log M), and the space complexity is O(log M). Here, n is the length of the array nums, and M is the maximum value of the elements in the array nums.

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

C#

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Convert to String Approach

Time Complexity: O(n), where n is the number of elements in the array.
Space Complexity: O(1), as we're using a fixed amount of extra space.

Logarithmic Approach

Time Complexity: O(n), the array is processed once.
Space Complexity: O(1), constant space utilized.

Simulation—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Convert to StringO(n)O(1)Best for readability and quick implementation when working with arrays
Logarithmic Digit CountO(n)O(1)Preferred when avoiding string conversion and demonstrating mathematical reasoning

Video Solution

LeetCode 1295: Find Numbers with Even Number of Digits - Interview Prep Ep 39 • Fisher Coder • 6,332 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Find Numbers with Even Number of Digits easy or hard?
LeetCode classifies this problem as Easy. The logic involves simple array traversal and digit counting. It is commonly used as a beginner exercise to practice iteration and basic math operations.
Find Numbers with Even Number of Digits Python/Java solution
In Python, iterate through the list and check len(str(num)) % 2 == 0. In Java, convert the number using String.valueOf(num) and check its length. Both implementations run in O(n) time and require only constant extra space.
How to solve Find Numbers with Even Number of Digits in O(n)?
Iterate through the array and compute the digit count for each number. You can convert the number to a string and check its length or compute digits using floor(log10(num)) + 1. Increment a counter whenever the digit count is even. The array is scanned once, giving O(n) time complexity.
What is the best approach for Find Numbers with Even Number of Digits?
The simplest approach converts each integer to a string and checks if the string length is even. This runs in O(n) time because each array element is processed once. An alternative uses the formula floor(log10(num)) + 1 to compute digit length mathematically, also in O(n) time and O(1) space.
Is Find Numbers with Even Number of Digits asked at Google/Amazon/Meta?
Problems like this appear frequently in screening rounds and coding assessments because they test basic array iteration and number manipulation. While the exact question may vary, similar easy problems are commonly used by companies such as Amazon and other large tech firms to evaluate coding fundamentals.
What data structure is used in Find Numbers with Even Number of Digits?
The primary data structure is an array that stores the integers. The algorithm simply iterates through the array and evaluates each value individually. No additional complex structures like hash maps or trees are required.
What is the time complexity of Find Numbers with Even Number of Digits?
The time complexity is O(n), where n is the number of elements in the array. Each number is inspected exactly once to determine its digit length. Digit counting itself is constant time because integers have a limited number of digits.

Ready to solve this problem?

Practice Find Numbers with Even Number of Digits with our built-in code editor and test cases.

Practice on FleetCode