1841. Odd Digit Count

Easy1.5 points estimatedMathsCountingSimulation3 hints
RPby Rohit Parihar1 submission

A meter reading is flagged for review when it has many odd digits. Given the integer n, return how many of its decimal digits are odd, that is 1, 3, 5, 7 or 9. A minus sign is ignored. The number 0 has no odd digits, and neither does any number written with only even digits.

Do not convert the number to text: look at the last digit with the remainder by 10, then remove it with integer division and repeat. Aim for O(log n) time and O(1) extra space.

Example 1

Input:
n = 1234567
Output:
4
Explanation:

The odd digits are 1, 3, 5 and 7.

Example 2

Input:
n = 2468
Output:
0
Explanation:

Every digit is even.

Example 3

Input:
n = -11
Output:
2
Explanation:

The sign is ignored and both digits are odd.

Constraints

-1015 ≤ n ≤ 1015

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(log n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…