#1295

Find Numbers with Even Number of Digits

Easy Digit counting

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Count how many numbers in the array have an even count of digits (for example 12 and 4321 qualify, 7 and 123 do not).

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: how many numbers have 2, 4, 6… digits?

Why this works: dividing by 10 chops off one digit, so counting digits takes about log10(x) steps. This is your first logarithmic loop.

Example: nums = [12, 345, 2, 6, 7896]

12   → 1 → 0          2 digits  ✓345  → 34 → 3 → 0     3 digits2    → 0              1 digit6    → 0              1 digit7896 → 789 → 78 → 7 → 0   4 digits ✓answer: 2

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Converting each number to a String works but hides the cost; also forgetting that 0 has one digit (not a concern here since values are positive, but remember it in general).