Skip to content
Related Articles

Related Articles

Count pairs with Even Product from two given arrays
  • Difficulty Level : Basic
  • Last Updated : 15 Dec, 2020

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:
Explanation: 
Pairs with even product are: { (arr[0], brr[1]), (arr[1], brr[0]), (arr[1], brr[1]), (arr[2], brr[1]) }. 
Therefore, the required output is 4.

Input: arr[] = { 3, 2, 1, 4, 4}, brr[] = { 1, 4, 2, 3, 1 } 
Output: 19

Naive Approach: The simplest approach to solve this problem is to traverse both the arrays and generate all possible pairs (arr[i], brr[j]) from both the arrays. For every pair, (arr[i], brr[j]), check if their product is an even number or not. If found to be true, then increment the count. Finally, print the count obtained. 



Time Complexity: O(N * M) 
Auxiliary Space: O(1)

Efficient Approach: The above approach can be optimized based on the following properties of product of two numbers:

Odd * Odd = Odd 
Even * Odd = Even 
Even * Even = Even 
 

Follow the steps below to solve the problem:

Below is the implementation of the above approach:

C++




// C++ program to implement
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to count pairs (arr[i], brr[j])
// whose product is an even number
int cntPairsInTwoArray(int arr[], int brr[],
                       int N, int M)
{
    // Stores count of odd
    // numbers in arr[]
    int cntOddArr = 0;
 
    // Stores count of odd
    // numbers in brr[]
    int cntOddBrr = 0;
 
    // Traverse the array, arr[]
    for (int i = 0; i < N; i++) {
 
        // If arr[i] is
        // an odd number
        if (arr[i] & 1) {
 
            // Update cntOddArr
            cntOddArr += 1;
        }
    }
 
    // Traverse the array, brr[]
    for (int i = 0; i < M; i++) {
 
        // If brr[i] is
        // an odd number
        if (brr[i] & 1) {
 
            // Update cntOddArr
            cntOddBrr += 1;
        }
    }
 
    // Return pairs whose product
    // is an even number
    return (N * M) - (cntOddArr * cntOddBrr);
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    int brr[] = { 1, 2 };
    int M = sizeof(brr) / sizeof(brr[0]);
 
    cout << cntPairsInTwoArray(arr, brr, N, M);
 
    return 0;
}

Java




// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to count pairs (arr[i], brr[j])
// whose product is an even number
static int cntPairsInTwoArray(int arr[], int brr[],
                       int N, int M)
{
   
    // Stores count of odd
    // numbers in arr[]
    int cntOddArr = 0;
 
    // Stores count of odd
    // numbers in brr[]
    int cntOddBrr = 0;
 
    // Traverse the array, arr[]
    for (int i = 0; i < N; i++) {
 
        // If arr[i] is
        // an odd number
        if (arr[i] % 2 == 1) {
 
            // Update cntOddArr
            cntOddArr += 1;
        }
    }
 
    // Traverse the array, brr[]
    for (int i = 0; i < M; i++) {
 
        // If brr[i] is
        // an odd number
        if (brr[i] % 2 == 1) {
 
            // Update cntOddArr
            cntOddBrr += 1;
        }
    }
 
    // Return pairs whose product
    // is an even number
    return (N * M) - (cntOddArr * cntOddBrr);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3 };
    int N = arr.length;
 
    int brr[] = { 1, 2 };
    int M = brr.length;
 
    System.out.print(cntPairsInTwoArray(arr, brr, N, M));
 
}
}
 
// This code is contributed by 29AjayKumar

Python3




# Python3 program to implement
# the above approach
 
# Function to count pairs (arr[i], brr[j])
# whose product is an even number
def cntPairsInTwoArray(arr, brr, N, M):
     
    # Stores count of odd
    # numbers in arr[]
    cntOddArr = 0
 
    # Stores count of odd
    # numbers in brr[]
    cntOddBrr = 0
 
    # Traverse the array, arr[]
    for i in range(N):
 
        # If arr[i] is
        # an odd number
        if (arr[i] & 1):
 
            # Update cntOddArr
            cntOddArr += 1
 
    # Traverse the array, brr[]
    for i in range(M):
 
        # If brr[i] is
        # an odd number
        if (brr[i] & 1):
 
            # Update cntOddArr
            cntOddBrr += 1
 
    # Return pairs whose product
    # is an even number
    return (N * M) - (cntOddArr * cntOddBrr)
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 2, 3 ]
    N = len(arr)
 
    brr = [ 1, 2 ]
    M = len(brr)
 
    print(cntPairsInTwoArray(arr, brr, N, M))
 
# This code is contributed by mohit kumar 29

C#




// C# program to implement
// the above approach 
using System;
    
class GFG{
    
// Function to count pairs (arr[i], brr[j])
// whose product is an even number
static int cntPairsInTwoArray(int[] arr, int[] brr,
                              int N, int M)
{
     
    // Stores count of odd
    // numbers in arr[]
    int cntOddArr = 0;
  
    // Stores count of odd
    // numbers in brr[]
    int cntOddBrr = 0;
  
    // Traverse the array, arr[]
    for(int i = 0; i < N; i++)
    {
         
        // If arr[i] is
        // an odd number
        if (arr[i] % 2 == 1)
        {
             
            // Update cntOddArr
            cntOddArr += 1;
        }
    }
  
    // Traverse the array, brr[]
    for(int i = 0; i < M; i++)
    {
         
        // If brr[i] is
        // an odd number
        if (brr[i] % 2 == 1)
        {
             
            // Update cntOddArr
            cntOddBrr += 1;
        }
    }
  
    // Return pairs whose product
    // is an even number
    return (N * M) - (cntOddArr * cntOddBrr);
}
    
// Driver Code
public static void Main()
{
    int[] arr = { 1, 2, 3 };
    int N = arr.Length;
  
    int[] brr = { 1, 2 };
    int M = brr.Length;
  
    Console.Write(cntPairsInTwoArray(
        arr, brr, N, M));
}
}
 
// This code is contributed by code_hunt
Output: 
4

 

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :