EasyNo. 1295

Find Numbers with Even Number of Digits — LeetCode 1295 Python Solution

Topics
Array, Math
Reading time
2 min

The problem

Given an array nums of integers, return how many of them contain an even number of digits.

Example

Input
nums = [12,345,2,6,7896]
Output
2
Explanation
12 contains 2 digits (even number of digits).

Python solution

PythonSolution
class Solution:
    def findNumbers(self, nums: List[int]) -> int:
        return sum(len(str(x)) % 2 == 0 for x in nums)

Complexity

Time
O(n × log M)
Space · auxiliary
O(log M)

Pattern: Math and Number Theory

Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 1295. Find Numbers with Even Number of Digits is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math.

The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.

Related problems

Related problems
No.ProblemDifficulty
1304Find N Unique Integers Sum up to ZeroEasy
0048Rotate ImageMedium
0066Plus OneEasy
0189Rotate ArrayMedium
0204Count PrimesMedium
0335Self CrossingHard

Frequently asked questions

How hard is LeetCode 1295. Find Numbers with Even Number of Digits?

LeetCode 1295. Find Numbers with Even Number of Digits is rated Easy on LeetCode.

What is the time complexity of LeetCode 1295. Find Numbers with Even Number of Digits?

The Python solution on this page runs in O(n × log M).

What is the space complexity of LeetCode 1295. Find Numbers with Even Number of Digits?

The Python solution on this page uses O(log M) auxiliary space.

What topics does LeetCode 1295. Find Numbers with Even Number of Digits cover?

LeetCode 1295. Find Numbers with Even Number of Digits is tagged Array and Math on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.