Open In App

Count Odd and Even

Last Updated : 06 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

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 == 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 % 2 == 1)
            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 % 2 == 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) {
          
            // checking if a number is completely divisible by 2
            if (num % 2 == 1)
                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] % 2 === 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
Article Tags :
Practice Tags :

Similar Reads