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:
6
6
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
4
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++
#include <iostream>
using namespace std;
void countPairs( int arr[], int n)
{
int even = 0, odd = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 == 0)
even++;
else
odd++;
}
cout << (even) * (n - 1) << endl;
cout << (odd) * (n - 1) << endl;
}
int main()
{
int arr[] = { 2, 3, 4, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
countPairs(arr, n);
return 0;
}
|
Java
class GFG
{
static void countPairs( int arr[], int n)
{
int even = 0 , odd = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] % 2 == 0 )
even++;
else
odd++;
}
System.out.println((even) * (n - 1 ));
System.out.println((odd) * (n - 1 ));
}
public static void main(String args[])
{
int arr[] = { 2 , 3 , 4 , 5 };
int n = arr.length;
countPairs(arr, n);
}
}
|
Python3
def countPairs(arr, n):
odd = 0
even = 0
for i in range (n):
if (arr[i] % 2 = = 0 ):
even + = 1
else :
odd + = 1
odd_pairs = odd * (n - 1 )
even_pairs = even * (n - 1 )
print (odd_pairs)
print (even_pairs)
if __name__ = = '__main__' :
arr = [ 2 , 3 , 4 , 5 ]
n = len (arr)
countPairs(arr, n)
|
C#
using System;
class GFG
{
static void countPairs( int []arr, int n)
{
int even = 0, odd = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
even++;
else
odd++;
}
Console.WriteLine((even) * (n - 1));
Console.WriteLine((odd) * (n - 1));
}
public static void Main()
{
int []arr = { 2, 3, 4, 5 };
int n = arr.Length;
countPairs(arr, n);
}
}
|
Javascript
<script>
function countPairs(arr, n)
{
let even = 0, odd = 0;
for (let i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
even++;
else
odd++;
}
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>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...