## Count Pairs from an Array with Even Product of Count of Distinct Prime Factor in JavaGiven 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.
int arr_A[] = {1, 7} int arr_B[] = {5, 6} int N = 2 int M = 2
The number of pairs from an array that form an even product with distinct prime factors is 1.
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.
int arr_A[] = {1, 2, 3} int arr_B[] = {4, 5, 6} int N = 3 int M = 3
The number of pairs from an array that form an even product with distinct prime factors is 2.
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 MethodThe 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
## Implementation:
The number of pairs from an array that form even product with distinct prime factors is 1
The above code's time complexity is O(N * log(N)), and its space complexity is O(N), where N represents the array length. |

For Videos Join Our Youtube Channel: Join Now

- Send your Feedback to [email protected]