Arrays and Hashing Array basics
50. Count the Odd Numbers
You are given an array of integers nums. Return how many of its elements are odd.
An integer is odd when it is not a multiple of 2. Negative numbers follow the same rule, so −7 is odd and −4 is even. Every element counts on its own, so a value that appears three times adds 3 to the answer if it is odd.
Example 1
Input: nums = [4, -3, 7, 0, -10, 9]
3Explanation: −3, 7 and 9 are odd. 4, 0 and −10 are multiples of 2, so they are even.
Example 2
Input: nums = [-5, -1, -7]
3Explanation: Every element is negative and none is a multiple of 2, so all three are odd.
Example 3
Input: nums = [0, 2, -4, 1000000000]
0Explanation: Every element is a multiple of 2, zero included, so no element is odd.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ nums.length ≤ 105-109 ≤ nums[i] ≤ 109
Clarifying questions
What you would ask an interviewer before writing any code.