# Count of even and odd power pairs in an Array

• Last Updated : 15 Feb, 2022

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:

Explanation: (2, 3), (2, 4), (2, 5), (4, 2), (4, 3) and (4, 5) are the pairs with even values
and (3, 2), (3, 4), (3, 5), (5, 2), (5, 3) and (5, 4) are the pairs with odd values.
Input: arr[] = {10, 11, 20, 60, 70}
Output:
16

Explanation: (10, 11), (10, 20), (10, 60), (10, 70), (20, 10), (20, 11), (20, 60), (20, 70), (60, 10), (60, 11), (60, 20), (60, 70), (70, 10), (70, 11), (70, 20), (70, 60) are the pairs with even values and (11, 10), (11, 20), (11, 60), (11, 70) are the pairs with odd values.

Naive approach: Calculate the powers for every single pair possible and find whether the calculated value is even or odd.
Efficient approach: Count the even and odd elements in the array and then use the concept pow (even, any element except itself) is even and pow (odd, any element except itself) is odd.
So, the number of pairs (X, Y) are,

• pow(X, Y) is even = (count of even number * (n – 1))
• pow(X, Y) is odd = (count of odd number * (n – 1))

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function to find and print the``// required count of pairs``void` `countPairs(``int` `arr[], ``int` `n)``{` `    ``// Find the count of even and``    ``// odd elements in the array``    ``int` `even = 0, odd = 0;``    ``for` `(``int` `i = 0; i < n; i++) {``        ``if` `(arr[i] % 2 == 0)``            ``even++;``        ``else``            ``odd++;``    ``}` `    ``// Print the required count of pairs``    ``cout << (even) * (n - 1) << endl;``    ``cout << (odd) * (n - 1) << endl;``}` `// Driver code``int` `main()``{``    ``int` `arr[] = { 2, 3, 4, 5 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);` `    ``countPairs(arr, n);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``class` `GFG``{` `    ``// Function to find and print the``    ``// required count of pairs``    ``static` `void` `countPairs(``int` `arr[], ``int` `n)``    ``{` `        ``// Find the count of even and``        ``// odd elements in the array``        ``int` `even = ``0``, odd = ``0``;``        ``for` `(``int` `i = ``0``; i < n; i++)``        ``{``            ``if` `(arr[i] % ``2` `== ``0``)``                ``even++;``            ``else``                ``odd++;``        ``}` `        ``// Print the required count of pairs``        ``System.out.println((even) * (n - ``1``));``        ``System.out.println((odd) * (n - ``1``));``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String args[])``    ``{``        ``int` `arr[] = { ``2``, ``3``, ``4``, ``5` `};``        ``int` `n = arr.length;` `        ``countPairs(arr, n);``    ``}``}` `// This code is contributed by ANKITUMAR34`

## Python3

 `# Python3 implementation of the approach` `# Function to find and print the``# required count of pairs``def` `countPairs(arr, n):``    ` `    ``# Find the count of even and``    ``# odd elements in the array``    ``odd ``=` `0``    ``even ``=` `0``    ``for` `i ``in` `range``(n):``        ``if` `(arr[i] ``%` `2` `=``=` `0``):``            ``even ``+``=` `1``        ``else``:``            ``odd ``+``=` `1``            ` `    ``# Count the number of odd pairs``    ``odd_pairs ``=` `odd``*``(n``-``1``)` `    ``# Count the number of even pairs``    ``even_pairs ``=` `even``*``(n``-``1``)` `    ``print``(odd_pairs)``    ``print``(even_pairs)` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ``arr ``=` `[``2``, ``3``, ``4``, ``5``]``    ``n ``=` `len``(arr)``    ``countPairs(arr, n)`

## C#

 `// C# implementation of the approach``using` `System;` `class` `GFG``{` `    ``// Function to find and print the``    ``// required count of pairs``    ``static` `void` `countPairs(``int` `[]arr, ``int` `n)``    ``{` `        ``// Find the count of even and``        ``// odd elements in the array``        ``int` `even = 0, odd = 0;``        ``for` `(``int` `i = 0; i < n; i++)``        ``{``            ``if` `(arr[i] % 2 == 0)``                ``even++;``            ``else``                ``odd++;``        ``}` `        ``// Print the required count of pairs``        ``Console.WriteLine((even) * (n - 1));``        ``Console.WriteLine((odd) * (n - 1));``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main()``    ``{``        ``int` `[]arr = { 2, 3, 4, 5 };``        ``int` `n = arr.Length;` `        ``countPairs(arr, n);``    ``}``}` `// This code is contributed by AnkitRai01`

## Javascript

 ``

Output:

```6
6```

Time Complexity: O(n)

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up