Find Numbers with Even Number of Digits - Problem

You're a data analyst working with a dataset of integers, and you need to perform a specific analysis: count how many numbers have an even number of digits.

Given an array nums of integers, return the count of numbers that contain an even number of digits. For example, the number 1234 has 4 digits (even), while 123 has 3 digits (odd).

Key Points:

  • Single digit numbers (0-9) have 1 digit (odd)
  • Two digit numbers (10-99) have 2 digits (even)
  • Three digit numbers (100-999) have 3 digits (odd)
  • And so on...

Your task is to efficiently count all numbers in the array that have 2, 4, 6, 8, ... digits.

Input & Output

example_1.py โ€” Basic Case
$ Input: [12,345,2,6,7896]
โ€บ Output: 2
๐Ÿ’ก Note: Numbers 12 (2 digits) and 7896 (4 digits) have even number of digits. Numbers 345 (3 digits), 2 (1 digit), and 6 (1 digit) have odd number of digits.
example_2.py โ€” Single Elements
$ Input: [555,901,482,1771]
โ€บ Output: 1
๐Ÿ’ก Note: Only number 1771 has 4 digits (even). Numbers 555, 901, and 482 all have 3 digits (odd).
example_3.py โ€” Edge Cases
$ Input: [0,10,100,1000]
โ€บ Output: 2
๐Ÿ’ก Note: Numbers 10 (2 digits) and 1000 (4 digits) have even digits. Numbers 0 (1 digit) and 100 (3 digits) have odd digits.

Constraints

  • 1 โ‰ค nums.length โ‰ค 500
  • 1 โ‰ค nums[i] โ‰ค 105
  • All integers are positive (no negative numbers in constraints)

Visualization

Tap to expand
Digital Digit Counter ProcessArray[12,345,67]ScannerReading: 12CounterDigits: 2โœ“EVEN!Next ScanNumber: 345ScannerReading: 345CounterDigits: 3โœ—ODDFinal Result Counter2 numbers with even digits(12: 2 digits, 67: 2 digits)
Understanding the Visualization
1
Scan Number
The scanner reads each number from the array
2
Count Digits
Digital counter determines digit count (like counting barcode stripes)
3
Check Parity
System checks if count is even (green light) or odd (red light)
4
Update Result
Even numbers increment our final counter
Key Takeaway
๐ŸŽฏ Key Insight: The problem reduces to efficiently counting digits and using the modulo operator to check parity - either through string conversion for simplicity or mathematical division for efficiency.
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
89.5K Views
Medium Frequency
~8 min Avg. Time
2.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen