📜  给定数组中偶数或奇数产品对的最大数量

📅  最后修改于: 2021-10-26 06:28:50             🧑  作者: Mango

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

例子:

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

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

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

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

下面是上述方法的实现:

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)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程