# 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 **X ^{Y}** is even and count the number of pairs such that

**X**is odd.

^{Y}**Examples:**

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

6

6Explanation:(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

4Explanation:(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 <iostream>` `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

`<script>` ` ` `// Javascript implementation of the approach` ` ` ` ` `// Function to find and print the` ` ` `// required count of pairs` ` ` `function` `countPairs(arr, n)` ` ` `{` ` ` ` ` `// Find the count of even and` ` ` `// odd elements in the array` ` ` `let even = 0, odd = 0;` ` ` `for` `(let i = 0; i < n; i++)` ` ` `{` ` ` `if` `(arr[i] % 2 == 0)` ` ` `even++;` ` ` `else` ` ` `odd++;` ` ` `}` ` ` ` ` `// Print the required count of pairs` ` ` `document.write((even) * (n - 1) + ` `"</br>"` `);` ` ` `document.write((odd) * (n - 1));` ` ` `}` ` ` ` ` `let arr = [ 2, 3, 4, 5 ];` ` ` `let n = arr.length;` ` ` `countPairs(arr, n);` ` ` `</script>` |

**Output:**

6 6

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. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.