VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-number-even-odd-elements-array/

⇱ Count Odd and Even - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count Odd and Even

Last Updated : 22 May, 2025

You are given an array arr[]. Your task is to count the number of even and odd elements. Return first odd count then even count.

Examples:

Input: arr = [2, 3, 4, 5, 6]
Output: 2 3
Explanation: There are two odds[3, 5] and three even[2, 4, 6] present in the array.

Input: arr = [22, 32, 42, 52, 62]
Output: 0 5
Explanation: All the elements are even.

By Using Mod Operator - O(n) Time and O(1) Space


Output
2 3

By Using AND Operator - O(n) Time and O(1) Space

We can also check if a number is odd or even by doing AND of 1 and that digit, if the result comes out to be 1 then the number is odd otherwise, it is even.


Output
2 3

Time Complexity: O(n)
Auxiliary Space: O(1) because it is using constant space for variables

Comment
Article Tags:
Article Tags: