Skip to content
Related Articles

Related Articles

Improve Article

Count Pairs from two arrays with even sum

  • Last Updated : 07 May, 2021

Given two arrays A[] and B[] of N and M integers respectively. The task is to count the number of unordered pairs formed by choosing an element from array A[] and other from array B[] in such a way that their sum is an even number. 
Note that an element will only be a part of a single pair.
Examples: 
 

Input: A[] = {9, 14, 6, 2, 11}, B[] = {8, 4, 7, 20} 
Output:
{9, 7}, {14, 8}, {6, 4} and {2, 20} are the valid pairs.
Input: A[] = {2, 4, 6}, B[] = {8, 10, 12} 
Output:
 

 

Approach: Count the number of odd and even numbers in both the arrays and the answer to the number of pairs will be min(odd1, odd2) + min(even1, even2) because (odd + odd) = even and (even + even) = even.
Below is the implementation of the above approach: 
 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return count of required pairs
int count_pairs(int a[], int b[], int n, int m)
{
 
    // Count of odd and even numbers
    // from both the arrays
    int odd1 = 0, even1 = 0;
    int odd2 = 0, even2 = 0;
 
    // Find the count of odd and
    // even elements in a[]
    for (int i = 0; i < n; i++) {
        if (a[i] % 2 == 1)
            odd1++;
        else
            even1++;
    }
 
    // Find the count of odd and
    // even elements in b[]
    for (int i = 0; i < m; i++) {
        if (b[i] % 2 == 1)
            odd2++;
        else
            even2++;
    }
 
    // Count the number of pairs
    int pairs = min(odd1, odd2) + min(even1, even2);
 
    // Return the number of pairs
    return pairs;
}
 
// Driver code
int main()
{
    int a[] = { 9, 14, 6, 2, 11 };
    int b[] = { 8, 4, 7, 20 };
    int n = sizeof(a) / sizeof(a[0]);
    int m = sizeof(b) / sizeof(b[0]);
    cout << count_pairs(a, b, n, m);
 
    return 0;
}

Java




// Java implementation of the approach
import java.io.*;
 
class GFG
{
 
// Function to return count of required pairs
static int count_pairs(int a[], int b[], int n, int m)
{
 
    // Count of odd and even numbers
    // from both the arrays
    int odd1 = 0, even1 = 0;
    int odd2 = 0, even2 = 0;
 
    // Find the count of odd and
    // even elements in a[]
    for (int i = 0; i < n; i++)
    {
        if (a[i] % 2 == 1)
            odd1++;
        else
            even1++;
    }
 
    // Find the count of odd and
    // even elements in b[]
    for (int i = 0; i < m; i++)
    {
        if (b[i] % 2 == 1)
            odd2++;
        else
            even2++;
    }
 
    // Count the number of pairs
    int pairs = Math.min(odd1, odd2) + Math.min(even1, even2);
 
    // Return the number of pairs
    return pairs;
}
 
// Driver code
public static void main (String[] args)
{
     
    int a[] = { 9, 14, 6, 2, 11 };
    int b[] = { 8, 4, 7, 20 };
    int n = a.length;
    int m = b.length;
    System.out.println (count_pairs(a, b, n, m));
 
}
}
 
// This code is contributes by ajit

Python3




# Python 3 implementation of the approach
 
# Function to return count of required pairs
def count_pairs(a,b,n,m):
     
    # Count of odd and even numbers
    # from both the arrays
     
    odd1 = 0
    even1 = 0
    odd2 = 0
    even2 = 0
     
    # Find the count of odd and
    # even elements in a[]
    for i in range(n):
        if (a[i] % 2 == 1):
            odd1 += 1
        else:
            even1 += 1
 
    # Find the count of odd and
    # even elements in b[]
     
    for i in range(m):
        if (b[i] % 2 == 1):
            odd2 += 1
        else:
            even2 += 1
             
    # Count the number of pairs
    pairs = min(odd1, odd2) + min(even1, even2)
     
    # Return the number of pairs
    return pairs
 
# Driver code
if __name__ == '__main__':
    a = [9, 14, 6, 2, 11]
    b = [8, 4, 7, 20]
    n = len(a)
    m = len(b)
    print(count_pairs(a, b, n, m))
 
# This code is contributed by
# Surendra_Gangwar

C#




// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return count of required pairs
static int count_pairs(int []a, int []b, int n, int m)
{
 
    // Count of odd and even numbers
    // from both the arrays
    int odd1 = 0, even1 = 0;
    int odd2 = 0, even2 = 0;
 
    // Find the count of odd and
    // even elements in a[]
    for (int i = 0; i < n; i++)
    {
        if (a[i] % 2 == 1)
            odd1++;
        else
            even1++;
    }
 
    // Find the count of odd and
    // even elements in b[]
    for (int i = 0; i < m; i++)
    {
        if (b[i] % 2 == 1)
            odd2++;
        else
            even2++;
    }
 
    // Count the number of pairs
    int pairs = Math.Min(odd1, odd2) + Math.Min(even1, even2);
 
    // Return the number of pairs
    return pairs;
}
 
// Driver code
public static void Main ()
{
     
    int []a = { 9, 14, 6, 2, 11 };
    int []b = { 8, 4, 7, 20 };
    int n = a.Length;
    int m = b.Length;
    Console.WriteLine (count_pairs(a, b, n, m));
 
}
}
 
// This code is contributes by anuj_67..

PHP




<?php
// PHP implementation of the approach
 
// Function to return count of required pairs
function count_pairs($a, $b, $n, $m)
{
 
    // Count of odd and even numbers
    // from both the arrays
    $odd1 = 0; $even1 = 0;
    $odd2 = 0; $even2 = 0;
 
    // Find the count of odd and
    // even elements in a[]
    for ($i = 0; $i < $n; $i++)
    {
        if ($a[$i] % 2 == 1)
            $odd1++;
        else
            $even1++;
    }
 
    // Find the count of odd and
    // even elements in b[]
    for ($i = 0; $i < $m; $i++)
    {
        if ($b[$i] % 2 == 1)
            $odd2++;
        else
            $even2++;
    }
 
    // Count the number of pairs
    $pairs = min($odd1, $odd2) + min($even1, $even2);
 
    // Return the number of pairs
    return $pairs;
}
 
// Driver code
$a = array( 9, 14, 6, 2, 11 );
$b = array( 8, 4, 7, 20 );
$n = count($a);
$m = count($b);
 
echo count_pairs($a, $b, $n, $m);
 
// This code is contributes by AnkitRai01
?>

Javascript




<script>
 
// Javascript implementation of the approach
 
    // Function to return count of required pairs
    function count_pairs(a , b , n , m)
    {
 
        // Count of odd and even numbers
        // from both the arrays
        var odd1 = 0, even1 = 0;
        var odd2 = 0, even2 = 0;
 
        // Find the count of odd and
        // even elements in a
        for (i = 0; i < n; i++) {
            if (a[i] % 2 == 1)
                odd1++;
            else
                even1++;
        }
 
        // Find the count of odd and
        // even elements in b
        for (i = 0; i < m; i++) {
            if (b[i] % 2 == 1)
                odd2++;
            else
                even2++;
        }
 
        // Count the number of pairs
        var pairs = Math.min(odd1, odd2) +
                    Math.min(even1, even2);
 
        // Return the number of pairs
        return pairs;
    }
 
    // Driver code
     
 
        var a = [ 9, 14, 6, 2, 11 ];
        var b = [ 8, 4, 7, 20 ];
        var n = a.length;
        var m = b.length;
        document.write(count_pairs(a, b, n, m));
 
 
// This code contributed by umadevi9616
 
</script>
Output: 
4

 

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :