Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Count Odd and Even
Next article icon

Count Odd and Even

Last Updated : 22 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

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.

Table of Content

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

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

C++
#include <iostream> #include <vector> using namespace std;  pair<int, int> countOddEven(vector<int>& arr) {    int countOdd = 0, countEven = 0;    for (int i = 0; i < arr.size(); i++) {             // checking if the element is even       if (arr[i] % 2 == 0) {          countEven++;       }             // if not even, it must be odd       else {          countOdd++;       }    }    return {countOdd, countEven}; }  int main() {    vector<int> arr = {2, 3, 4, 5, 6};    pair<int, int> ans = countOddEven(arr);    cout << ans.first << " " << ans.second;    return 0; } 
Java
class GfG {    static int[] countOddEven(int[] arr) {       int countOdd = 0, countEven = 0;       for (int i = 0; i < arr.length; i++) {          // checking if the element is even          if (arr[i] % 2 == 0) {             countEven++;          } else {             // if not even, it must be odd             countOdd++;          }       }       return new int[] {countOdd, countEven};    }     public static void main(String[] args) {       int[] arr = {2, 3, 4, 5, 6};       int[] ans = countOddEven(arr);       System.out.println(ans[0] + " " + ans[1]);    } } 
Python
def countOddEven(arr):     countOdd = 0     countEven = 0     for num in arr:                # checking if the element is even         if num % 2 == 0:             countEven += 1         else:                        # if not even, it must be odd             countOdd += 1     return countOdd, countEven  if __name__ == "__main__":   arr = [2, 3, 4, 5, 6]   ans = countOddEven(arr)   print(ans[0], ans[1]) 
C#
using System; using System.Collections.Generic;  class GfG {     static Tuple<int, int> CountOddEven(int[] arr) {         int countOdd = 0, countEven = 0;         for (int i = 0; i < arr.Length; i++) {                        // checking if the element is even             if (arr[i] % 2 == 0) {                 countEven++;             } else {                                // if not even, it must be odd                 countOdd++;             }         }         return Tuple.Create(countOdd, countEven);     }      public static void Main(string[] args) {         int[] arr = { 2, 3, 4, 5, 6 };         var ans = CountOddEven(arr);         Console.WriteLine(ans.Item1 + " " + ans.Item2);     } } 
JavaScript
function countOddEven(arr) {     let countOdd = 0, countEven = 0;     for (let i = 0; i < arr.length; i++) {              // checking if the element is even         if (arr[i] % 2 === 0) {             countEven++;         } else {                      // if not even, it must be odd             countOdd++;         }     }     return [countOdd, countEven]; }  // Driver Code const arr = [2, 3, 4, 5, 6]; const ans = countOddEven(arr); console.log(ans[0], ans[1]); 

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.

C++
#include <iostream> #include <vector> using namespace std;  pair<int, int> countOddEven(vector<int>& arr) {    int evenCount = 0, oddCount = 0;    for (int i = 0; i < arr.size(); i++) {             // checking if a number is completely divisible by 2       if (arr[i] & 1)          oddCount++;       else          evenCount++;    }    return {oddCount, evenCount}; }  int main() {    vector<int> arr = {2, 3, 4, 5, 6};    pair<int, int> ans = countOddEven(arr);    cout << ans.first << " " << ans.second; } 
Java
import java.util.Arrays;  class GfG {    static int[] countOddEven(int[] arr) {       int evenCount = 0, oddCount = 0;       for (int num : arr) {          // checking if a number is completely divisible by 2          if ((num & 1) != 0)             oddCount++;          else             evenCount++;       }       return new int[] {oddCount, evenCount};    }     public static void main(String[] args) {       int[] arr = {2, 3, 4, 5, 6};       int[] ans = countOddEven(arr);       System.out.println(ans[0] + " " + ans[1]);    } } 
Python
def countOddEven(arr):     evenCount = 0     oddCount = 0     for num in arr:                # checking if a number is completely divisible by 2         if (num & 1):             oddCount += 1         else:             evenCount += 1     return oddCount, evenCount  if __name__ == "__main__":   arr = [2, 3, 4, 5, 6]   ans = countOddEven(arr)   print(ans[0], ans[1]) 
C#
using System;  class GfG {     static int[] CountOddEven(int[] arr) {         int evenCount = 0, oddCount = 0;         foreach (int num in arr) {                                 if ((num & 1) != 0)                 oddCount++;             else                 evenCount++;         }         return new int[] { oddCount, evenCount };     }      public static void Main(string[] args) {         int[] arr = { 2, 3, 4, 5, 6 };         int[] ans = CountOddEven(arr);         Console.WriteLine(ans[0] + " " + ans[1]);     } } 
JavaScript
function countOddEven(arr) {     let evenCount = 0, oddCount = 0;     for (let i = 0; i < arr.length; i++) {              // checking if a number is completely divisible by 2         if (arr[i] & 1)             oddCount++;         else             evenCount++;     }     return [oddCount, evenCount]; }  // Driver Code const arr = [2, 3, 4, 5, 6]; const ans = countOddEven(arr); console.log(ans[0], ans[1]); 

Output
2 3

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


Next Article
Count Odd and Even

M

mohitw16
Improve
Article Tags :
  • Computer Science Fundamentals
  • DSA
  • Arrays
Practice Tags :
  • Arrays

Similar Reads

    Count even and odd digits in an Integer
    A certain number is given and the task is to count even digits and odd digits of the number and also even digits are present even a number of times and, similarly, for odd numbers. Print Yes If: If number contains even digits even number of time Odd digits odd number of times Else Print No Examples
    13 min read
    Count rotations of N which are Odd and Even
    Given a number n, the task is to count all rotations of the given number which are odd and even. Examples: Input: n = 1234Output: Odd = 2, Even = 2Total rotations: 1234, 2341, 3412, 4123Odd rotations: 2341 and 4123Even rotations: 1234 and 3412Input: n = 246Output: Odd = 0, Even = 3 Brute force appro
    7 min read
    Counting 1's Surrounded by Even 0's
    Given an 'n x m' matrix composed solely of '0's and '1's, the task is to determine the count of '1's that are enclosed by an even number (greater than zero) of neighboring '0's. The surroundings of a matrix cell encompass the eight neighboring cells, including those diagonally adjacent. Examples: In
    11 min read
    Count of even and odd power pairs in an Array
    Given an array arr[] of length N, the task is to count the number of pairs (X, Y) such that XY is even and count the number of pairs such that XY is odd.Examples: Input: arr[] = {2, 3, 4, 5} Output: 6 6 Explanation: (2, 3), (2, 4), (2, 5), (4, 2), (4, 3) and (4, 5) are the pairs with even values and
    5 min read
    Count odd and even digits in a number in PL/SQL
    Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations.Given a number and task is to find the number of odd and
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences