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 (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,

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 contributd by ANKITUMAR34
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 contributd by AnkitRai01
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)
chevron_right

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.





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.



Improved By : ANKITKUMAR34, AnkitRai01

Article Tags :