Talentd
FleetCode
Make and follow sheets.Sign in

Find Numbers with Even Number of Digits solution and explanation

EasyArrayMath5 min read79.7% acceptanceUpdated 9 Apr 2026
LeetCode 1295: Find Numbers with Even Number of Digits - Interview Prep Ep 39Fisher Coder · 4:34 · 6K viewsWatch on YouTube
9 more videos on this problem
  1. LeetCode 1295: Find Numbers with Even Number of Digits - Interview Prep Ep 39Fisher Coder · 4:34 · 6K views
  2. Find Numbers With Even Number Of Digits | Multiple Ways | Leetcode 1295 | codestorywithMIKcodestorywithMIK · 11:12 · 3K views
  3. Leetcode | 1295. Find Numbers with Even Number of Digits | Easy | Java SolutionDeveloper Docs · 4:10 · 3K views
  4. LeetCode - 1295. Find Numbers with Even Number of Digits | Arrays CardAditya Mahajan · 2:51 · 1K views
  5. Leetcode 1295: Find Numbers with Even Number of DigitsAlgorithms Casts · 5:36 · 791 views
  6. leetcode - 1295 Find Numbers with Even Number of Digits JavaScript SolutionlitCode · 4:25 · 482 views
  7. Daily Leetcode #1856: Apr 30, 2025 - 1295. Find Numbers with Even Number of DigitsProgramming Live with Larry · 6:01 · 460 views
  8. Leetcode 1295. Find Numbers with Even Number of Digits | Python Easy Problem ExplainedStatLearn Tech · 3:51 · 387 views
  9. Simple C++ Solution - LeetCode #1295 Find Numbers with Even Number of DigitsJoshProgramin · 3:50 · 229 views
  10. Find Numbers with Even number of digits | Leetcode 1295 | Zero to FAANG DSA BootcampHAIL CODING · 3:27 · 226 views

The problem

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.

def findNumbers(nums):
    count = 0
    for num in nums:
        if len(str(num)) % 2 == 0:
            count += 1
    return count

nums = [12, 345, 2, 6, 7896]
print(findNumbers(nums))
class Solution {
    public int findNumbers(int[] nums) {
        int count = 0;
        for (int num : nums) {
            if (String.valueOf(num).length() % 2 == 0) {
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        Solution sol = new Solution();
        int[] nums = {12, 345, 2, 6, 7896};
        System.out.println(sol.findNumbers(nums));
    }
}
#include <iostream>
#include <string>
using namespace std;

int findNumbers(vector<int>& nums) {
    int count = 0;
    for (int num : nums) {
        if (to_string(num).length() % 2 == 0) {
            count++;
        }
    }
    return count;
}

int main() {
    vector<int> nums = {12, 345, 2, 6, 7896};
    cout << findNumbers(nums) << endl;
    return 0;
}
function findNumbers(nums) {
    let count = 0;
    for (let num of nums) {
        if (num.toString().length % 2 === 0) {
            count++;
        }
    }
    return count;
}

const nums = [12, 345, 2, 6, 7896];
console.log(findNumbers(nums));
#include <stdio.h>
#include <string.h>

int findNumbers(int* nums, int numsSize) {
    int count = 0;
    for (int i = 0; i < numsSize; i++) {
        char str[10];
        sprintf(str, "%d", nums[i]);
        if (strlen(str) % 2 == 0) {
            count++;
        }
    }
    return count;
}

int main() {
    int nums[] = {12, 345, 2, 6, 7896};
    int size = sizeof(nums) / sizeof(nums[0]);
    printf("%d\n", findNumbers(nums, size));
    return 0;
}
using System;

class Program {
    static int FindNumbers(int[] nums) {
        int count = 0;
        foreach (var num in nums) {
            if (num.ToString().Length % 2 == 0) {
                count++;
            }
        }
        return count;
    }

    static void Main() {
        int[] nums = {12, 345, 2, 6, 7896};
        Console.WriteLine(FindNumbers(nums));
    }
}

This Python function iterates over the list, converts each number to a string, and increments the count if the length of the string (number of digits) is even.

Complexity

Time Complexity: O(n), where n is the length of the numbers array. We are iterating over the array once.
Space Complexity: O(1), additional space doesn't grow with input size.

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.

import math

def findNumbers(nums):
    count = 0
    for num in nums:
        if (int(math.log10(num)) + 1) % 2 == 0:
            count += 1
    return count

nums = [12, 345, 2, 6, 7896]
print(findNumbers(nums))
class Solution {
    public int findNumbers(int[] nums) {
        int count = 0;
        for (int num : nums) {
            if ((int)(Math.log10(num) + 1) % 2 == 0) {
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        Solution sol = new Solution();
        int[] nums = {12, 345, 2, 6, 7896};
        System.out.println(sol.findNumbers(nums));
    }
}
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

int findNumbers(vector<int>& nums) {
    int count = 0;
    for (int num : nums) {
        if (static_cast<int>(log10(num)) % 2 == 1) {
            count++;
        }
    }
    return count;
}

int main() {
    vector<int> nums = {12, 345, 2, 6, 7896};
    cout << findNumbers(nums) << endl;
    return 0;
}
function findNumbers(nums) {
    let count = 0;
    for (let num of nums) {
        if (Math.floor(Math.log10(num) + 1) % 2 === 0) {
            count++;
        }
    }
    return count;
}

const nums = [12, 345, 2, 6, 7896];
console.log(findNumbers(nums));
#include <stdio.h>
#include <math.h>

int findNumbers(int* nums, int numsSize) {
    int count = 0;
    for (int i = 0; i < numsSize; i++) {
        if ((int)log10(nums[i]) % 2 == 1) {
            count++;
        }
    }
    return count;
}

int main() {
    int nums[] = {12, 345, 2, 6, 7896};
    int size = sizeof(nums) / sizeof(nums[0]);
    printf("%d\n", findNumbers(nums, size));
    return 0;
}
using System;

class Program {
    static int FindNumbers(int[] nums) {
        int count = 0;
        foreach (var num in nums) {
            if ((int)Math.Floor(Math.Log10(num) + 1) % 2 == 0) {
                count++;
            }
        }
        return count;
    }

    static void Main() {
        int[] nums = {12, 345, 2, 6, 7896};
        Console.WriteLine(FindNumbers(nums));
    }
}

In Python, the math.log10() function is used to determine the number of digits in a number. By flooring the result and adding 1, we get the digit count, which is checked for evenness.

Complexity

Time Complexity: O(n), we loop through each element once.
Space Complexity: O(1), due to constant space allocation.

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.

function findNumbers(nums: number[]): number {
    return nums.filter(x => x.toString().length % 2 === 0).length;
}
func findNumbers(nums []int) (ans int) {
	for _, x := range nums {
		if len(strconv.Itoa(x))%2 == 0 {
			ans++
		}
	}
	return
}
impl Solution {
    pub fn find_numbers(nums: Vec<i32>) -> i32 {
        nums.iter().filter(|&x| x.to_string().len() % 2 == 0).count() as i32
    }
}
Try this approach in the editor

Complexity comparison

ApproachComplexity
Convert to String Approach

Time Complexity: O(n), where n is the length of the numbers array. We are iterating over the array once.
Space Complexity: O(1), additional space doesn't grow with input size.

Logarithmic Approach

Time Complexity: O(n), we loop through each element once.
Space Complexity: O(1), due to constant space allocation.

Simulation–

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

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.

Comments

Now write it yourselfClose this page and solve Find Numbers with Even Number of Digits in the editor, without looking.
Practise Find Numbers with Even Number of Digits