Foundations Working with digits and divisors
28. Count the Odd Digits
Given an integer n, return how many of its digits are odd. The odd digits are 1, 3, 5, 7 and 9.
Every digit counts on its own, so a 5 that appears three times adds 3 to the answer. A negative number has the same digits as its absolute value, because the minus sign is not a digit. The number 0 has a single digit, 0, which is even, so the answer for 0 is 0.
Example 1
Input: n = 7305921
5Explanation: The digits are 7, 3, 0, 5, 9, 2 and 1. Of these, 7, 3, 5, 9 and 1 are odd.
Example 2
Input: n = -3355
4Explanation: The minus sign is not a digit. All four digits, 3, 3, 5 and 5, are odd, and each one counts.
Example 3
Input: n = 0
0Explanation: 0 has one digit, and 0 is even.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
-9 × 1015 ≤ n ≤ 9 × 1015
Clarifying questions
What you would ask an interviewer before writing any code.