给定两个由N 个整数组成的数组A[]和B[] ,任务是计算具有偶数和奇数乘积的对(A[i], B[j])并打印两个计数中的最大值。
例子:
Input: A[] = {1, 2, 3}, B[] = {4, 5, 6}
Output: 7
Explanation:
Pairs with odd product count are: (1, 5) (3, 5). Therefore, number of pairs with odd product = 2.
Pairs with even product count are: (1, 4) (1, 6) (2, 4) (2, 5) (2, 6) (3, 4) (3, 6). Therefore, number of pairs with even product = 7
Therefore, the maximum of the two counts is 7. Hence, the output 7.
Input: A[] = {1, 5, 9}, B[] = {1, 7, 3}
Output: 9
Explanation:
Pairs with odd product count are: (1, 1) (1, 7) (1, 3) (5, 1) (5, 7) (5, 3) (9, 1) (9, 7) (9, 3). Therefore, number of pairs with odd product = 9
No pair with even product count exists.
Therefore, the output 9.
朴素的方法:最简单的方法是生成所有可能的对并检查它们的乘积是偶数还是奇数,并相应地增加它们各自的计数。检查所有对后,打印这两个计数中的最大值。
时间复杂度: O(N 2 )
辅助空间: O(1)
有效的解决方案:可以通过观察仅将一对奇数相乘得到奇数来优化上述方法。否则,将生成偶数整数。请按照以下步骤解决问题:
- 初始化变量奇数,偶数, 和oddCountB为0 ,分别存储数组B[]中的奇数对、偶数对和奇数对的个数。
- 如果元素为奇数,则遍历数组B[]并递增oddCountB 。现在(N –oddCountB)的值给出了数组B[]中偶数元素的数量。
- 遍历数组A[]并执行以下操作:
- 如果当前元素是偶数,则将 evencount增加N,因为乘以B[] 中的任何数字将产生偶数乘积对。
- 否则,递增evencount通过–因为在B中的偶数(N oddCountB)[]当与A []奇数相乘,得到(N – oddCountB)对和由oddCountB增量oddcount,因为在B []乘以奇数在A[] 中有一个奇数给出奇数产品对的计数。
- 完成以上步骤后,打印oddcount和evencount的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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live