给定两个分别由N和M个整数组成的数组A []和B [] 。任务是计算通过从数组A []中选择一个元素以及从数组B []中选择另一个元素而形成的无序对的数量,以使它们的总和为偶数。
请注意,一个元素将仅是一对的一部分。
例子:
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
方法:对两个数组中的奇数和偶数进行计数,对数的答案将是min(odd1,odd2)+ min(even1,even2),因为(odd + odd)=偶数和(even +偶数) )=偶数。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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
输出:
4