Count Odd and Even Last Updated : 22 May, 2025 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice 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 5Explanation: All the elements are even.Table of ContentBy Using Mod Operator - O(n) Time and O(1) SpaceBy Using AND Operator - O(n) Time and O(1) SpaceBy 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]); Output2 3By Using AND Operator - O(n) Time and O(1) SpaceWe 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]); Output2 3Time Complexity: O(n)Auxiliary Space: O(1) because it is using constant space for variables Comment More infoAdvertise with us Next Article Count Odd and Even M mohitw16 Follow 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 Like