Count pairs in an array containing at least one even value

Given an array arr[], the task is to count pairs such that each pair (arr[i], arr[j]) contains at least one even element in it where i != j.

Examples:

Input: arr[] = {1, 2, 3, 1, 3}
Output: 4
Explanation:
Possible pairs are: (1, 2), (2, 3), (2, 1), (2, 3).

Input: arr[] = {8, 2, 3, 1, 4, 2}
Output: 14
Explanation:
Possible pairs are: (8, 2), (8, 3), (8, 1), (8, 4), (8, 2), (2, 3), (2, 1), (2, 4), (2, 2), (3, 4), (3, 2), (1, 4), (1, 2), (4, 2).

A Simple Approach is to run two loops. Pick each element one-by-one and for each element find element on right side of array that holds condition, then increment count.



Time Complexity:

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to count 
// pairs in an array such that 
// each pair contains at 
// least one even element 
#include<bits/stdc++.h> 
using namespace std; 
   
// Function to count the pairs in 
// the array such as there is at 
// least one even element in each pair 
int CountPairs(int arr[], int n) 
    int count = 0; 
   
    // Generate all possible pairs 
    // and increment then count 
    // if the condition is satisfied 
    for(int i = 0; i < n; i++)
    {
       for(int j = i + 1; j < n; j++)
       
          if (arr[i] % 2 == 0 || 
              arr[j] % 2 == 0) 
              count++; 
       
    
    return count; 
   
// Driver code 
int main() 
    int arr[] = { 8, 2, 3, 1, 4, 2 }; 
    int n = sizeof(arr) / sizeof(int); 
   
    // Function call 
    cout << (CountPairs(arr, n)); 
   
// This code is contributed by rock_cool 
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to Count
// pairs in an array such that
// each pair contains at
// least one even element
import java.util.*;
  
class GFG {
  
    // Function to count the pairs in
    // the array such as there is at
    // least one even element in each pair
    static int CountPairs(int[] arr, int n)
    {
  
        int count = 0;
  
        // Generate all possible pairs
        // and increment then count
        // if the condition is satisfied
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
  
                if (arr[i] % 2 == 0
                    || arr[j] % 2 == 0)
                    count++;
            }
        }
  
        return count;
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        int[] arr = { 8, 2, 3, 1, 4, 2 };
        int n = arr.length;
  
        // Function Call
        System.out.println(CountPairs(arr, n));
    }
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to count 
# pairs in an array such that 
# each pair contains at 
# least one even element 
def CountPairs(arr, n):
      
    count = 0
      
    # Generate all possible pairs 
    # and increment then count 
    # if the condition is satisfied 
    for i in range(n):
        for j in range(i + 1, n):
            if (arr[i] % 2 == 0 or 
                arr[j] % 2 == 0):
                count += 1
                  
    return count
      
# Driver code 
arr = [ 8, 2, 3, 1, 4, 2 ]
n = len(arr)
  
# Function call
print(CountPairs(arr, n))
  
# This code is contributed by rutvik_56
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to count 
// pairs in an array such that 
// each pair contains at 
// least one even element 
using System; 
   
class GFG{ 
   
// Function to count the pairs in 
// the array such as there is at 
// least one even element in each pair 
static int CountPairs(int[] arr, int n) 
   
    int count = 0; 
   
    // Generate all possible pairs 
    // and increment then count 
    // if the condition is satisfied 
    for(int i = 0; i < n; i++) 
    
       for(int j = i + 1; j < n; j++)
       
          if (arr[i] % 2 == 0 || 
              arr[j] % 2 == 0) 
              count++; 
       
    
    return count; 
   
// Driver code 
public static void Main(String[] args) 
    int[] arr = { 8, 2, 3, 1, 4, 2 }; 
    int n = arr.Length; 
   
    // Function Call 
    Console.WriteLine(CountPairs(arr, n)); 
   
// This code is contributed by PrinciRaj1992 
chevron_right

Output:
14

Efficient Approach: The idea is to count the even and odd elements in the array and include pairs having only one even element or both the pairs to be even element.

Therefore, the count of the pairs having at least one even element will be

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to Count
// pairs in an array such that
// each pair contains at
// least one even element
#include <bits/stdc++.h> 
using namespace std;
  
// Function to count the pairs in
// the array such as there is at
// least one even element in each pair
int CountPairs(int arr[], int n)
{
      
    // Store count of even
    // and odd elements
    int even = 0, odd = 0;
    for(int i = 0; i < n; i++)
    {
          
       // Check element is
       // even or odd
       if (arr[i] % 2 == 0)
           even++;
       else
           odd++;
    }
    return (even * (even - 1)) / 2 +
           (even * odd);
}
  
// Driver Code
int main()
{
    int arr[] = { 8, 2, 3, 1, 4, 2 };
    int n = sizeof(arr) / sizeof(int);
      
    cout << CountPairs(arr, n);
}
  
// This code is contributed by jrishabh99
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to Count
// pairs in an array such that
// each pair contains at
// least one even element
import java.util.*;
  
class GFG {
  
    // Function to count the pairs in
    // the array such as there is at
    // least one even element in each pair
    static int CountPairs(int[] arr, int n)
    {
        // strore count of even
        // and odd elements
        int even = 0, odd = 0;
  
        for (int i = 0; i < n; i++) {
  
            // check element is
            // even or odd
            if (arr[i] % 2 == 0)
                even++;
            else
                odd++;
        }
  
        return (even * (even - 1)) / 2
            + (even * odd);
    }
  
    // Driver Code
    public static void main(String[] args)
    {
  
        int[] arr = { 8, 2, 3, 1, 4, 2 };
        int n = arr.length;
        System.out.println(CountPairs(arr, n));
    }
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to Count 
// pairs in an array such that 
// each pair contains at 
// least one even element 
using System; 
class GFG{ 
   
// Function to count the pairs in 
// the array such as there is at 
// least one even element in each pair 
static int CountPairs(int[] arr, int n) 
       
    // Store count of even 
    // and odd elements 
    int even = 0, odd = 0; 
   
    for(int i = 0; i < n; i++) 
    
          
       // Check element is 
       // even or odd 
       if (arr[i] % 2 == 0) 
           even++; 
       else
           odd++; 
    
    return (even * (even - 1)) / 2 + 
           (even * odd); 
   
// Driver Code 
public static void Main() 
    int[] arr = { 8, 2, 3, 1, 4, 2 }; 
    int n = arr.Length; 
       
    Console.Write(CountPairs(arr, n)); 
   
// This code is contributed by Nidhi_biet 
chevron_right

Output:
14

Time Complexity: O(N)
Space Complexity: O(1)

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.



Article Tags :
Practice Tags :