📌  相关文章
📜  从两个给定数组中计算偶数对

📅  最后修改于: 2021-09-07 02:20:40             🧑  作者: Mango

给定大小分别为NM 的两个数组arr[]brr[] ,任务是找到对(arr[i], brr[j])的计数,使得对的元素的乘积是偶数数字。

例子:

朴素的方法:解决这个问题的最简单的方法是遍历两个数组并从两个数组中生成所有可能的对(arr[i], brr[j]) 。对于每一对(arr[i], brr[j]) ,检查它们的乘积是否为偶数。如果发现为真,则增加计数。最后,打印获得的计数。

时间复杂度: O(N * M)
辅助空间: O(1)

高效方法:上述方法可以根据两个数的乘积的以下特性进行优化:

请按照以下步骤解决问题:

  • 初始化两个变量,比如cntOddArrcntOddBrr ,以分别存储数组arr[]brr[] 中存在的奇数计数。
  • 遍历数组arr[]并对每个数组元素,检查它是否为奇数。如果发现为真,则更新cntOddArr += 1
  • 遍历数组brr[]并对于每个数组元素索引,检查它是否是奇数。如果发现为真,则更新cntOddBrr += 1
  • 最后,打印((N * M) – cntOddArr * cntOddBrr) 的值

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
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


Javascript


输出:
4

时间复杂度: O(N)
空间复杂度: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live