# Count Pairs from an Array with Even Product of Count of Distinct Prime Factor in Java

Given two arrays, A[] and B[], such that each array contains N and M integers particularly. Our task is to find out the count pairings (A[i], B[j]), which ensure that the product of their count of distinct prime factors is even.

Example 1:

Input:

int arr_A[] = {1, 7}

int arr_B[] = {5, 6}

int N = 2

int M = 2

Output:

The number of pairs from an array that form an even product with distinct prime factors is 1.

Explanation:

For a given arrays arr_A[] = {1, 7} and arr_B[] = {5, 6}; such that the arrays are modified to arr_A[] = {0, 1} and arr_B[] = {1, 2} by substituting the count of each element in the array with the element of its separate prime factor. Therefore, {7, 6} is the pair with an even product. Hence, the total number of formed pairs is 1.

Example 2:

Input:

int arr_A[] = {1, 2, 3}

int arr_B[] = {4, 5, 6}

int N = 3

int M = 3

Output:

The number of pairs from an array that form an even product with distinct prime factors is 2.

Explanation:

For a given arrays arr_A[] = {1, 2, 3} and arr_B[] = {4, 5, 6}; such that the arrays are modified to arr_A[] = {0, 1, 1} and arr_B[] = {1, 1, 2} by substituting the count of each element in the array with the element of its separate prime factor. Therefore, {{2, 6}, {3, 6}} are the total pairs with even product. Hence, the total formed pairs are 2.

## Approach: Using the Sieve of Eratosthenes Method

The method efficiently calculates separate prime factors using the Sieve of Eratosthenes. It then counts the even and odd prime factor values to find the number of pairs that have an even product of prime factors. This strategy can be made more effective by using the following property of the product of two numbers to optimize the count of unique prime factors of all the numbers up to the greatest element from both arrays:

Odd * Odd = Odd

Even * Odd = Even

Odd * Even = Even

Even * Even = Even

Algorithm:

Step 1: First, find the distinct prime factors for every number up to MAX, then store the results in a vector called countDistinct in vector<int>.

Step 2: Initialize two variables, such as evenCnt and oddCnt, to hold the array elements' count of different prime factors that are even and odd, respectively, in arr_B[].

Step 3: Iterate through the arr_B[] array.

Step 3.1: Go to the next step if the count[arr_B[i]] = 0. Increment oddCnt by one if it's odd.

Step 3.2: Increment evenCnt by one if not.

Step 4: Initialize the value of the variable evenPairs to zero.

Step 5: If count[arr_A[i]] is odd, traverse the array arr_A[] and increase evenPairs by evenCnt.

Step 6: If not, multiply evenCnt by oddCnt to increase evenPairs.

Step 7: Display the evenPairs value.

## Implementation:

FileName: CountPairsWithProducts.java

Output:

The number of pairs from an array that form even product with distinct prime factors is 1

Complexity Analysis:

The above code's time complexity is O(N * log(N)), and its space complexity is O(N), where N represents the array length.