Count Pairs from two arrays with even sum
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 ?> |
4
Recommended Posts:
- Given two arrays count all pairs whose sum is an odd number
- Count pairs from two arrays having sum equal to K
- Count equal pairs from given string arrays
- Count distinct pairs from two arrays having same sum of digits
- Count pairs from two sorted arrays whose sum is equal to a given value x
- Count of pairs between two arrays such that the sums are distinct
- Count pairs from two arrays whose modulo operation yields K
- Count pairs formed by distinct element sub-arrays
- Given two unsorted arrays, find all pairs whose sum is x
- Maximizing Unique Pairs from two arrays
- Distinct pairs from given arrays (a[i], b[j]) such that (a[i] + b[j]) is a Fibonacci number
- Find k pairs with smallest sums in two arrays
- Minimum sum of absolute difference of pairs of two arrays
- Total distinct pairs of ugly numbers from two arrays
- Count pairs with given sum | Set 2
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.



