📜  给定阵列的最大偶数或奇数产品对计数

📅  最后修改于: 2021-05-17 22:37:16             🧑  作者: Mango

给定两个由N个整数组成的数组A []B [] ,任务是对具有偶数和奇数乘积的对(A [i],B [j])进行计数,并打印两个计数中的最大值。

例子:

天真的方法:最简单的方法是生成所有可能的对,并检查它们的乘积是偶数还是奇数,并相应地增加其各自的计数。检查所有对之后,打印这两个计数中的最大值。

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

高效的解决方案:可以通过观察仅乘以一对奇数整数得出奇数来优化上述方法。否则,将生成一个偶数整数。请按照以下步骤解决问题:

  • 初始化变量oddcountevencount oddCountB0存储奇数产物对,甚至产品对的计数,和奇数号的在阵列B上分别[]计数。
  • 遍历数组B [] ,如果元素为奇数,则递增oddCountB 。现在, (N –奇数计数B)的值给出了数组B []中偶数个元素的数量。
  • 遍历数组A []并执行以下操作:
    • 如果当前元素为偶数,则将N增加偶数计数,因为将B []中的任何数字相乘将得出偶数乘积对。
    • 否则,递增evencount通过因为在B中的偶数(N oddCountB)[]当与A []奇数相乘,得到(N – oddCountB)对和由oddCountB增量oddcount,因为在B []乘以奇数在A []中带有奇数的数字给出奇数乘积对的计数。
  • 完成上述步骤后,打印出奇数偶数计数的最大值作为结果。

下面是上述方法的实现:

C++
// C++ program for above approach
 
#include 
using namespace std;
 
// Function to return the maximum
// count of odd count pair or
// even count pair
int maxcountpair(int A[], int B[], int N)
{
    // Stores odd count, even count
    // pairs and odd numbers in B[]
    int oddcount = 0, evencount = 0;
    int oddCountB = 0;
 
    // Traverse the array B[]
    for (int i = 0; i < N; i++) {
 
        // If B[i] is an odd number
        if (B[i] & 1)
 
            // Increment oddCountB by 1
            oddCountB += 1;
    }
 
    // Traverse the array A[]
    for (int i = 0; i < N; i++) {
 
        // If A[i] is an odd number
        if (A[i] & 1) {
            oddcount += oddCountB;
            evencount += (N - oddCountB);
        }
 
        // If A[i] is even
        else
            evencount += N;
    }
 
    // Return maximum count of odd
    // and even pairs
    return max(oddcount, evencount);
}
 
// Driver Code
int main()
{
    int A[] = { 1, 2, 3 };
    int B[] = { 4, 5, 6 };
 
    int N = sizeof(A) / sizeof(A[0]);
 
    // Function Call
    cout << maxcountpair(A, B, N);
 
    return 0;
}


C
// C program for above approach
#include 
 
// Function to return the
// maximum of two numbers
int max(int num1, int num2)
{
    return (num1 > num2) ? num1 : num2;
}
 
// Function to return the maximum count of
// odd count pair or even count pair
int maxcountpair(int A[], int B[], int N)
{
     
    // Stores odd count, even count
    // pairs and odd numbers in B[]
    int oddcount = 0, evencount = 0;
    int oddCountB = 0;
     
    // Traverse the array B[]
    for(int i = 0; i < N; i++)
    {
         
        // If B[i] is an odd number
        if (B[i] & 1)
         
            // Increment oddCountB by 1
            oddCountB += 1;
    }
 
    // Traverse the array A[]
    for(int i = 0; i < N; i++)
    {
         
        // If A[i] is an odd number
        if (A[i] & 1)
        {
            oddcount += oddCountB;
            evencount += (N - oddCountB);
        }
         
        // If A[i] is even
        else
            evencount += N;
    }
 
    // Return maximum count of odd
    // and even pairs
    return max(oddcount, evencount);
}
 
// Driver Code
int main()
{
    int A[] = { 1, 2, 3 };
    int B[] = { 4, 5, 6 };
 
    int N = sizeof(A) / sizeof(A[0]);
     
    printf("%d", maxcountpair(A, B, N));
 
    return 0;
}
 
// This code is contributed by sourav singh


Java
// Java program for above approach
import java.io.*;
 
class GFG{
 
// Function to return the maximum count of
// odd count pair or even count pair
static int maxcountpair(int A[], int B[],
                        int N)
{
     
    // Stores odd count, even count
    // pairs and odd numbers in B[]
    int oddcount = 0, evencount = 0;
    int oddCountB = 0;
 
    // Traverse the array B[]
    for(int i = 0; i < N; i++)
    {
         
        // If B[i] is an odd number
        if (B[i] % 2 == 1)
         
            // Increment oddCountB by 1
            oddCountB += 1;
    }
 
    // Traverse the array A[]
    for(int i = 0; i < N; i++)
    {
         
        // If A[i] is an odd number
        if (A[i] % 2 == 1)
        {
            oddcount += oddCountB;
            evencount += (N - oddCountB);
        }
 
        // If A[i] is even
        else
            evencount += N;
    }
 
    // Return maximum count of odd
    // and even pairs
    return (oddcount > evencount) ? oddcount
                                  : evencount;
}
 
// Driver Code
public static void main(String[] args)
{
    int A[] = { 1, 2, 3 };
    int B[] = { 4, 5, 6 };
    int N = A.length;
 
    System.out.print(maxcountpair(A, B, N));
}
}
 
// This code is contributed by sourav singh


Python3
# Python3 program for above approach
 
# Function to return the maximum count of
# odd count pair or even count pair
def maxcountpair(A, B, N):
     
    # Function to return the maximum count of
    # odd count pair or even count pair
    oddcount = 0
    evencount = 0
    oddCountB = 0
     
    # Traverse the array, B[]
    for i in range(0, N):
         
        # If B[i] is an odd number
        if B[i] % 2 == 1:
            oddCountB += 1
             
    # Traverse the array, A[]
    for i in range(0, N):
         
        # If A[i] is an odd number
        if A[i] % 2 == 1:
            oddcount += oddCountB
            evencount += (N - oddCountB)
        else:
            evencount += N
 
    # Return maximum count of odd
    # and even pairs
    return max(oddcount, evencount)
 
# Driver Code
A = [ 1, 2, 3 ]
B = [ 4, 5, 6 ]
N = len(A)
 
print(maxcountpair(A, B, N))
 
# This code is contributed by sourav singh


C#
// C# program for above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to return the maximum count of
// odd count pair or even count pair
static int maxcountpair(int[] A, int[] B,
                        int N)
{
     
    // Stores odd count, even count
    // pairs and odd numbers in B[]
    int oddcount = 0, evencount = 0;
    int oddCountB = 0;
 
    // Traverse the array B[]
    for(int i = 0; i < N; i++)
    {
         
        // If B[i] is an odd number
        if (B[i] % 2 == 1)
         
            // Increment oddCountB by 1
            oddCountB += 1;
    }
 
    // Traverse the array A[]
    for(int i = 0; i < N; i++)
    {
         
        // If A[i] is an odd number
        if (A[i] % 2 == 1)
        {
            oddcount += oddCountB;
            evencount += (N - oddCountB);
        }
 
        // If A[i] is even
        else
            evencount += N;
    }
 
    // Return maximum count of odd
    // and even pairs
    return (oddcount > evencount) ? oddcount
                                  : evencount;
}
 
// Driver Code
public static void Main(String[] args)
{
    int[] A = { 1, 2, 3 };
    int[] B = { 4, 5, 6 };
    int N = A.Length;
     
    // Function call
    Console.Write(maxcountpair(A, B, N));
}
}
 
// This code is contributed by sourav singh


Javascript


输出:
7

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