Count of all possible pairs of array elements with same parity

Given an array A[] of integers, the task is to find the total number of pairs such that each pair contains either both even or both odd elements. A vaild pair (A[ i ], A[ j ]) can only be formed if i != j.

Examples:

Input: A[ ] = {1, 2, 3, 1, 3}
Output: 6
Explanation:
Possible odd pairs = (1, 3), (1, 1), (1, 3), (3, 1), (3, 3), (1, 3) = 6
Possible even pairs = 0
Hence, total pairs = 6 + 0 = 6

Input: A[ ] = {8, 2, 3, 1, 4, 2}
Output: 7
Explanation:
Possible odd pair = (3, 1) = 1
Possible even pairs = (8, 2), (8, 4), (8, 2), (2, 4), (2, 2), (4, 2) = 6
Hence, total pairs = 6 + 1 = 7

Naive Approach:
The simplest approach is to generate all possible pairs. For each pair, check if both elements are odd or both are even. If so, increment a counter. The final count will be the required answer.
Time Complexity: O(N2)



Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for above approach
  
import java.util.*;
  
class GFG {
  
    static int countPairs(
        int[] A, int n)
    {
        int count = 0, i, j;
  
        // Generate all possible pairs
        for (i = 0; i < n; i++) {
            for (j = i + 1; j < n; j++) {
  
                // Increment the count if
                // both even or both odd
                if ((A[i] % 2 == 0
                     && A[j] % 2 == 0)
                    || (A[i] % 2 != 0
                        && A[j] % 2 != 0))
                    count++;
            }
        }
        return count;
    }
  
    // Driver Code
    public static void main(
        String[] args)
    {
        int[] A = { 1, 2, 3, 1, 3 };
        int n = A.length;
        System.out.println(
            countPairs(A, n));
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for above approach
using System;
class GFG{
  
static int countPairs(int[] A, int n)
{
    int count = 0, i, j;
  
    // Generate all possible pairs
    for (i = 0; i < n; i++)
    {
        for (j = i + 1; j < n; j++) 
        {
  
            // Increment the count if
            // both even or both odd
            if ((A[i] % 2 == 0 && A[j] % 2 == 0) || 
                (A[i] % 2 != 0 && A[j] % 2 != 0))
                count++;
        }
    }
    return count;
}
  
// Driver Code
public static void Main(String[] args)
{
    int[] A = { 1, 2, 3, 1, 3 };
    int n = A.Length;
    Console.Write(countPairs(A, n));
}
}
  
// This code is contributed by shivanisinghss2110

chevron_right


Output:

6

Efficient Approach:
Traverse the array and count and store even and odd numbers in the array and calculate the possible pairs from respective counts and display their sum.

Let the count of even and odd elements in the array be EC and OC respectively.
Count of even pairs = ( EC * ( EC – 1 ) ) / 2
Count of odd pairs = ( OC * ( OC – 1 ) ) / 2
Hence, total number of possible pairs = (( EC * ( EC – 1 ) ) + ( OC * ( OC – 1 ) ))/ 2

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
  
import java.util.*;
  
class GFG {
  
    static int countPairs(
        int[] A, int n)
    {
        // Store count of
        // even and odd elements
        int even = 0, odd = 0;
  
        for (int i = 0; i < n; i++) {
  
            if (A[i] % 2 == 0)
                even++;
            else
                odd++;
        }
  
        return (even * (even - 1)) / 2
            + (odd * (odd - 1)) / 2;
    }
  
    // Driver Program
    public static void main(
        String[] args)
    {
  
        int[] A = { 1, 2, 3, 1, 3 };
        int n = A.length;
        System.out.println(
            countPairs(A, n));
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
  
class GFG{
  
static int countPairs(int[] A, int n)
{
      
    // Store count of
    // even and odd elements
    int even = 0, odd = 0;
  
    for(int i = 0; i < n; i++) 
    {
       if (A[i] % 2 == 0)
           even++;
       else
           odd++;
    }
  
    return (even * (even - 1)) / 2 + 
            (odd * (odd - 1)) / 2;
}
  
// Driver code
public static void Main()
{
    int[] A = { 1, 2, 3, 1, 3 };
    int n = A.Length;
      
    Console.Write(countPairs(A, n));
}
}
  
// This code is contributed by nidhi_biet

chevron_right


Output:

6

Time complexity: O(N)

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.



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.