# Find the count of even odd pairs in a given Array

Given an array arr[], the task is to find the count even-odd pairs in the array.

Examples:

Input: arr[] = { 1, 2, 1, 3 }
Output: 2
Explanation:
The 2 pairs of the form (even, odd) are {2, 1} and {2, 3}.

Input: arr[] = { 5, 4, 1, 2, 3}
Output: 3

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach:

1. Run two nested loops to get all the possible pairs of numbers in the array.
2. For each pair, check whether if a[i] is even and a[j] is odd.
3. If it is, then increase the required count by one.
4. When all the pairs have been checked, print the count in the end.

Below is the implementation of the above approach:

## C++

 `// C++ program to count the pairs in array ` `// of the form (even, odd) ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to count the pairs in array ` `// of the form (even, odd) ` `int` `findCount(``int` `arr[], ``int` `n) ` `{ ` `    ``// variable to store count of such pairs ` `    ``int` `res = 0; ` ` `  `    ``// Iterate through all pairs ` `    ``for` `(``int` `i = 0; i < n - 1; i++) ` `        ``for` `(``int` `j = i + 1; j < n; j++) ` ` `  `            ``// Increment count ` `            ``// if condition is satisfied ` `            ``if` `((arr[i] % 2 == 0) ` `                ``&& (arr[j] % 2 == 1)) { ` `                ``res++; ` `            ``} ` ` `  `    ``return` `res; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `a[] = { 5, 4, 1, 2, 3 }; ` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a); ` `    ``cout << findCount(a, n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to count the pairs in array ` `// of the form (even, odd) ` `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `// Function to count the pairs in array ` `// of the form (even, odd) ` `static` `int` `findCount(``int` `arr[], ``int` `n) ` `{ ` `    ``// Variable to store count of such pairs ` `    ``int` `res = ``0``; ` ` `  `    ``// Iterate through all pairs ` `    ``for` `(``int` `i = ``0``; i < n - ``1``; i++) ` `        ``for` `(``int` `j = i + ``1``; j < n; j++) ` ` `  `            ``// Increment count ` `            ``// if condition is satisfied ` `            ``if` `((arr[i] % ``2` `== ``0``) && ` `                ``(arr[j] % ``2` `== ``1``)) ` `            ``{ ` `                ``res++; ` `            ``} ` `    ``return` `res; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `a[] = { ``5``, ``4``, ``1``, ``2``, ``3` `}; ` `    ``int` `n = a.length; ` `    ``System.out.print(findCount(a, n)); ` `} ` `} ` ` `  `// This code is contributed by Rohit_ranjan `

## Python3

 `# Python3 program to count the pairs  ` `# in array of the form (even, odd)  ` ` `  `# Function to count the pairs in   ` `# array of the form (even, odd)  ` `def` `findCount(arr, n): ` ` `  `    ``# Variable to store count ` `    ``# of such pairs  ` `    ``res ``=` `0` ` `  `    ``# Iterate through all pairs  ` `    ``for` `i ``in` `range``(``0``, n ``-` `1``):  ` `        ``for` `j ``in` `range``(i ``+` `1``, n):  ` ` `  `            ``# Increment count  ` `            ``# if condition is satisfied  ` `            ``if` `((arr[i] ``%` `2` `=``=` `0``) ``and`  `                ``(arr[j] ``%` `2` `=``=` `1``)): ` `                ``res ``=` `res ``+` `1` ` `  `    ``return` `res ` ` `  `# Driver code  ` `a ``=` `[ ``5``, ``4``, ``1``, ``2``, ``3` `]  ` `n ``=` `len``(a) ` ` `  `print``(findCount(a, n)) ` ` `  `# This code is contributed by PratikBasu `

## C#

 `// C# program to count the pairs in array ` `// of the form (even, odd) ` `using` `System; ` ` `  `class` `GFG{ ` ` `  `// Function to count the pairs in array ` `// of the form (even, odd) ` `static` `int` `findCount(``int` `[]arr, ``int` `n) ` `{ ` ` `  `    ``// Variable to store count of such pairs ` `    ``int` `res = 0; ` ` `  `    ``// Iterate through all pairs ` `    ``for``(``int` `i = 0; i < n - 1; i++) ` `       ``for``(``int` `j = i + 1; j < n; j++) ` `        `  `          ``// Increment count ` `          ``// if condition is satisfied ` `          ``if` `((arr[i] % 2 == 0) && ` `              ``(arr[j] % 2 == 1)) ` `          ``{ ` `              ``res++; ` `          ``} ` `           `  `    ``return` `res; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `[]a = { 5, 4, 1, 2, 3 }; ` `    ``int` `n = a.Length; ` `     `  `    ``Console.Write(findCount(a, n)); ` `} ` `} ` ` `  `// This code is contributed by Rohit_ranjan `

Output:

```3
```

Efficient Approach:

1. For each element starting from index 0 we will check if it’s even or not.
2. If it’s even we will increase count by 1
3. Else we will increase our final answer by count.

Below is the implementation of the above approach:

## C++

 `// C++ program to count the pairs in array ` `// of the form (even, odd) ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to count the pairs in array ` `// of the form (even, odd) ` `int` `findCount(``int` `arr[], ``int` `n) ` `{ ` `    ``int` `count = 0, ans = 0; ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// check if number is even or not ` `        ``if` `(arr[i] % 2 == 0) ` `            ``count++; ` `        ``else` `            ``ans = ans + count; ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `a[] = { 5, 4, 1, 2, 3 }; ` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a); ` `    ``cout << findCount(a, n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to count the pairs  ` `// in array of the form (even, odd) ` `class` `GFG{ ` ` `  `// Function to count the pairs in   ` `// array of the form (even, odd) ` `static` `int` `findCount(``int` `arr[], ``int` `n) ` `{ ` `    ``int` `count = ``0``, ans = ``0``; ` `    ``for``(``int` `i = ``0``; i < n; i++)  ` `    ``{ ` `         `  `       ``// Check if number is even  ` `       ``// or not ` `       ``if` `(arr[i] % ``2` `== ``0``) ` `       ``{ ` `           ``count++; ` `       ``} ` `       ``else` `       ``{ ` `           ``ans = ans + count; ` `       ``} ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `a[] = { ``5``, ``4``, ``1``, ``2``, ``3` `}; ` `    ``int` `n = a.length; ` `     `  `    ``System.out.print(findCount(a, n)); ` `} ` `} ` ` `  `// This code is contributed by amal kumar choubey `

## Python3

 `# Python3 program to count the pairs  ` `# in array of the form (even, odd)  ` ` `  `# Function to count the pairs in  ` `# array of the form (even, odd)  ` `def` `findCount(arr, n): ` ` `  `    ``count ``=` `0` `    ``ans ``=` `0` `     `  `    ``for` `i ``in` `range``(``0``, n): ` ` `  `        ``# Check if number is even or not  ` `        ``if` `(arr[i] ``%` `2` `=``=` `0``):  ` `            ``count ``=` `count ``+` `1` `        ``else``: ` `            ``ans ``=` `ans ``+` `count ` `             `  `    ``return` `ans  ` ` `  `# Driver code  ` `a ``=` `[ ``5``, ``4``, ``1``, ``2``, ``3` `] ` `n ``=` `len``(a)  ` ` `  `print``(findCount(a, n)) ` ` `  `# This code is contributed by PratikBasu `

## C#

 `// C# program to count the pairs in  ` `// array of the form (even, odd) ` `using` `System; ` ` `  `class` `GFG{ ` ` `  `// Function to count the pairs in  ` `// array of the form (even, odd) ` `static` `int` `findCount(``int` `[]arr, ``int` `n) ` `{ ` `    ``int` `count = 0, ans = 0; ` `    ``for``(``int` `i = 0; i < n; i++)  ` `    ``{ ` ` `  `       ``// Check if number is even or not ` `       ``if` `(arr[i] % 2 == 0) ` `       ``{ ` `           ``count++;    ` `       ``} ` `       ``else` `       ``{ ` `           ``ans = ans + count; ` `       ``} ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `[]a = { 5, 4, 1, 2, 3 }; ` `    ``int` `n = a.Length; ` `     `  `    ``Console.WriteLine(findCount(a, n)); ` `} ` `} ` ` `  `// This code is contributed by Code_Mech `

Output:

```3
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.