Prerequisite: To Find Maximum and Minimum Prime Number
Given four integers A, B, C and X, the task is to minimize the cost of selecting two numbers N and M such that the product of N and M is equal to X, i.e. N * M = X. The cost to select the numbers N and M is decided as follows:
- For the first number N:
- The cost is A if N is a prime number.
- The cost is B if N is a composite number.
- The cost is C if N is 1.
- For the second number M (!= 1), the cost is M.
Input: A = 7, B = 11, C = 2, X = 20
The following are the possible values and the cost for each pair:
Let N = 1 and M = 20, cost used in selecting N as 1 is C = 2, Total cost = 2 + 20 = 22.
Let N = 2 and M = 10, cost used in selecting N as prime number is A = 7, Total cost = 7 + 10 = 17
Let N = 4 and M = 5, cost used in selecting N as composite number is B = 11, Total cost = 11 + 5 = 15
Let N = 5 and M = 4, cost used in selecting N as prime number is A = 7, Total cost = 7 + 4 = 11
Let N = 10 and M = 2, cost used in selecting N as composite number is B = 11, Total cost = 11 + 2 = 13
Minimum among all the above is 11.
Input: A = 1, B = 1, C = 1, X = 40
The minimum cost is when N = 20 and M = 2, Total cost = 1 + 2 = 3.
Efficient Approach: There can be three possible cases for N as follows:
- N is Prime: Then the first number cost is fixed. In order to minimize the cost, the highest prime number possible is chosen such that N ≠ X.
- N is Composite: Similar to the above case, the maximum composite number that divides the number, but not the number itself has to be found. In order to do this, the minimum prime number that divides X is found and this is considered as M and cost is computed.
- N is 1: Any number can be formed(except when X = 1) and M = X for this case.
Therefore, the idea is to compute the cost for all the three cases and find the minimum among all. In order to do this, the list of all the minimum prime factors and maximum prime factors is precomputed using a slight variation of Sieve of Eratosthenes and stored in an array. The cost for all the three cases can be easily computed from these arrays.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimize positive product of two given numbers by at most N decrements
- Minimize cost to convert given two integers to zero using given operations
- Minimize cost to Swap two given Arrays
- Predict the winner of the game on the basis of absolute difference of sum by selecting numbers
- Minimize product of first N - 1 natural numbers by swapping same positioned bits of pairs
- Minimize the cost to split a number
- Minimize the cost of buying the Objects
- Minimize the cost of partitioning an array into K groups
- Minimize Cost to sort a String in Increasing Order of Frequencies of Characters
- Minimize cost to empty a given string by removing characters alphabetically
- Minimize cost to convert a given matrix to another by flipping columns and reordering rows
- Minimize cost to convert all characters of a binary string to 0s
- Minimize cost of choosing and skipping array elements to reach end of the given array
- Ways of selecting men and women from a group to make a team
- Total ways of selecting a group of X men from N men with or without including a particular man
- Maximum Subset Sum possible by negating the entire sum after selecting the first Array element
- Maximum Sum possible by selecting X elements from a Matrix based on given conditions
- Maximize sum by selecting M elements from the start or end of rows of a Matrix
- Minimize K whose XOR with given array elements leaves array unchanged
- Find number of factors of N when location of its two factors whose product is N is given
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : AnkitRai01