Count Pairs from an Array with Even Product of Count of Distinct Prime Factor in Java10 Sept 2024 | 4 min read 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 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 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. |
What is ? Spark is a Java micro framework that allows to quickly create web applications in Java 8. Spark is a lightweight and simple Java web framework designed for quick development. Sinatra, a popular Ruby micro framework, was the inspiration for it. Spark makes considerable use of...
8 min read
In this section, we will learn what is a Goldbach number and also create Java programs to check if the given number is Goldbach or not. The Goldbach number Java program frequently asked in Java coding tests to check the logic of the programmer. In 1742, German...
5 min read
Java, being one of the most widely used programming languages, provides a robust exception handling mechanism to help developers identify and handle runtime errors effectively. Exception messages play a crucial role in this process, as they provide valuable information about the nature and cause of the...
4 min read
? Transport Layer Security (TLS) is a protocol that ensures privacy between communicating applications and their users on the Internet. Setting the appropriate TLS version when developing Java applications is crucial for ensuring secure communication. Java TLS configuration is essential for applications like financial services, healthcare...
5 min read
Concurrency is a fundamental aspect of modern software development, and Java provides several mechanisms to handle concurrent tasks efficiently. Two commonly used synchronization tools in Java are CyclicBarrier and CountDownLatch. Despite their similar-sounding names, these classes serve distinct purposes in managing concurrent operations. In this section,...
4 min read
Structure of Class in Memory Every single class of a Java program gets converted into bytecode when the Java program gets compiled. The main purpose of bytecode is to store the instructions that will be executed by the Java Virtual Machine (JVM). The Class object is responsible...
8 min read
In this section, we will discuss the vertical order traversal of a binary tree in Java and the different approaches to achieve it. In the vertical order traversal, we print the node of the binary tree vertically, i.e., from top to bottom. For example, consider the...
8 min read
The multithreading capability of Java is an effective tool that enhances program performance and resource economy by enabling many threads to run simultaneously. The idea of thread hierarchies, which offer an organized method of managing concurrent work, is the foundation of Java's threading paradigm. This section...
5 min read
? Java multithreading enables the concurrent operation of several threads within a program. But when several threads use the same resources, problems like inconsistent data and racial situations can occur. Java provides synchronization techniques to solve these issues. Synchronized Keyword An essential component of Java's synchronization is the synchronized...
6 min read
Like C++, Java also supports the copy constructor. But in C++, it is created by default. In Java, we define copy constructors ourselves. Constructor In Java, a constructor is the same as a method, but the only difference is that the constructor has the same name as...
10 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India