Find Numbers with Even Number of Digits - Problem

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

A number has an even number of digits if the count of its digits is divisible by 2.

Example: The number 123 has 3 digits (odd), while 1234 has 4 digits (even).

Input & Output

Example 1 — Basic Mixed Array
$ Input: nums = [12,345,2,6,7896]
Output: 2
💡 Note: 12 has 2 digits (even), 345 has 3 digits (odd), 2 has 1 digit (odd), 6 has 1 digit (odd), 7896 has 4 digits (even). Total numbers with even digits: 2
Example 2 — All Single Digits
$ Input: nums = [7,8]
Output: 0
💡 Note: 7 has 1 digit (odd), 8 has 1 digit (odd). No numbers have even digit count.
Example 3 — Large Numbers
$ Input: nums = [555,901,482,1771]
Output: 1
💡 Note: 555 has 3 digits (odd), 901 has 3 digits (odd), 482 has 3 digits (odd), 1771 has 4 digits (even). Only 1771 has even digits.

Constraints

  • 1 ≤ nums.length ≤ 500
  • 1 ≤ nums[i] ≤ 105

Visualization

Tap to expand
Even Digit Counter - Mathematical Approach INPUT Array of Integers: 12 345 2 6 7896 [0] [1] [2] [3] [4] Digit Counts: 12 --> 2 digits 345 --> 3 digits 2 --> 1 digit 6 --> 1 digit 7896 --> 4 digits ALGORITHM STEPS 1 Count Digits Use log10(num)+1 or divide by 10 repeatedly 2 Check Even digitCount % 2 == 0 means even digits 3 Increment Counter If even, add 1 to result counter 4 Return Count Return total numbers with even digits Processing: 12: 2%2=0 [EVEN] 345: 3%2=1 [ODD] 7896: 4%2=0 [EVEN] FINAL RESULT Numbers with Even Digits: 12 2 digits 7896 4 digits Excluded (Odd Digits): 345 2 6 Output: 2 OK - 2 numbers have even digit counts Key Insight: The mathematical approach uses log10(n)+1 to find digit count in O(1) time per number. For n=0, handle separately. Total complexity: O(n) where n is array length. No extra space needed. TutorialsPoint - Find Numbers with Even Number of Digits | Mathematical Approach
Asked in
Google 15 Amazon 12 Microsoft 8
21.6K Views
Medium Frequency
~15 min Avg. Time
892 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