Arrays and Hashing Array basics

50. Count the Odd Numbers

Easy

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]

Output: 3

Explanation: −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]

Output: 3

Explanation: Every element is negative and none is a multiple of 2, so all three are odd.

Example 3

Input: nums = [0, 2, -4, 1000000000]

Output: 0

Explanation: 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.

nums = [-3, -1, 0, 2, 5]

Constraints

  • 1 ≤ nums.length ≤ 105
  • -109 ≤ nums[i] ≤ 109

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.