Count Pairs from two arrays with even sum
Last Updated :
31 May, 2022
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: 4
{9, 7}, {14, 8}, {6, 4} and {2, 20} are the valid pairs.
Input: A[] = {2, 4, 6}, B[] = {8, 10, 12}
Output: 3
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>
Time Complexity: O(n + m)
Auxiliary Space: O(1)
Similar Reads
Count pairs with Even Product from two given arrays Given two arrays, arr[] and brr[] of size N and M respectively, the task is to find the count of pairs (arr[i], brr[j]) such that the product of elements of the pairs is an even number. Examples: Input: arr[] = { 1, 2, 3 }, brr[] = { 1, 2 } Output: 4 Explanation: Pairs with even product are: { (arr[
10 min read
Count pairs from two arrays having sum equal to K Given an integer K and two arrays A1 and A2, the task is to return the total number of pairs (one element from A1 and one element from A2) with a sum equal to K. Note: Arrays can have duplicate elements. We consider every pair as different, the only constraint is, an element (of any array) can parti
6 min read
Count pairs from two arrays with difference exceeding K Given two integer arrays arr[] and brr[] consisting of distinct elements of size N and M respectively and an integer K, the task is to find the count of pairs(arr[i], brr[j]) such that (brr[j] - arr[i]) > K. Examples: Input: arr[] = {5, 9, 1, 8}, brr[] {10, 12, 7, 4, 2, 3}, K = 3 Output: 6 Explan
9 min read
Given two arrays count all pairs whose sum is an odd number Given two arrays of N and M integers. The task is to find the number of unordered pairs formed of elements from both arrays in such a way that their sum is an odd number. Note: An element can only be one pair.Examples: Input: a[] = {9, 14, 6, 2, 11}, b[] = {8, 4, 7, 20} Output: 3 {9, 20}, {14, 7} an
7 min read
2 Sum - Count Pairs with given Sum in Sorted Array Given a sorted array arr[] and an integer target, the task is to find the number of pairs in the array whose sum is equal to target.Examples: Input: arr[] = [-1, 1, 5, 5, 7], target = 6Output: 3Explanation: Pairs with sum 6 are (1, 5), (1, 5) and (-1, 7). Input: arr[] = [1, 1, 1, 1], target = 2Outpu
9 min read