Find Numbers with Even Number of Digits - Complete Solution Guide
Find Numbers with Even Number of Digits is LeetCode problem 1295, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
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 Ex
Detailed Explanation
The core task in "Find Numbers with Even Number of Digits" is straightforward: iterate through a given list of integers, `nums`, and for each number, determine if it possesses an even count of digits. The ultimate goal is to return a total tally of such numbers. For instance, if `nums` contains `[12, 345, 2, 6, 7896]`, we'd be looking to identify `12` (two digits) and `7896` (four digits) as contributors to our final count of 2.
Solution Approach
The provided solution embraces a wonderfully pragmatic and highly readable approach: leveraging string conversion. For each integer `num` in the `nums` array, the algorithm first casts `num` into its string representation using `str(num)`. Once it's a string, determining the number of digits becomes trivial—it's simply the `len()` of that string. The elegance here is that built-in string functions handle all the heavy lifting of digit counting, including potential edge cases like zero (which would be '0' with length 1). After obtaining the length, a simple modulo operation (`% 2 == 0`) efficiently checks for an even digit count, incrementing a `count` variable if true. This method sidesteps manual arithmetic loops, resulting in concise and idiomatic code.
Step-by-Step Algorithm
- Step 1: Initialize a counter variable `count` to 0.
- Step 2: Iterate through each number `num` in the input array `nums`.
- Step 3: Determine the number of digits in `num` (using either string conversion or repeated division).
- Step 4: Check if the number of digits is even using the modulo operator (`% 2 == 0`).
- Step 5: If the number of digits is even, increment the `count`.
- Step 6: After iterating through all numbers, return the final value of `count`.
Key Insights
- The most direct and often idiomatic way to count digits in many programming languages is through string conversion. `len(str(number))` instantly provides the digit count, obviating the need for iterative division by 10.
- The problem simplifies the "even/odd" digit count check to a basic modulo 2 operation (`length % 2 == 0`), which is extremely efficient and immediately reveals the parity of the digit count.
- The solution's cleanliness stems from its clear functional decomposition: one language feature (`str()`) for digit representation, another (`len()`) for counting, and an operator (`%`) for parity checking. This, combined with direct iteration, makes the code highly readable and maintainable.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Array, Math.
Companies
Asked at: Quora.