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.
Examples:
Input: A = 7, B = 11, C = 2, X = 20
Output: 11
Explanation:
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
Output: 3
Explanation:
The minimum cost is when N = 20 and M = 2, Total cost = 1 + 2 = 3.
Naive Approach: Find all the factors of the number and check if the factor is prime or not, accordingly find the cost and select minimum from them.
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:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100000;
int max_prime[MAX];
int min_prime[MAX];
void sieve( int n)
{
for ( int i = 2; i <= n; ++i) {
if (min_prime[i] > 0) {
continue ;
}
min_prime[i] = i;
max_prime[i] = i;
int j = i + i;
while (j <= n) {
if (min_prime[j] == 0) {
min_prime[j] = i;
}
max_prime[j] = i;
j += i;
}
}
}
int findCost( int A, int B, int C, int X)
{
sieve(MAX);
int N, M;
if (X == 1) {
return -1;
}
int min_cost = C + X;
int cost_for_prime = A;
N = max_prime[X];
if (N != X) {
M = X / N;
cost_for_prime += M;
min_cost = min(min_cost, cost_for_prime);
}
M = min_prime[X];
N = X / M;
if (N != min_prime[N]) {
int cost_for_comp = B + M;
min_cost = min(min_cost, cost_for_comp);
}
return min_cost;
}
int main()
{
int A = 7, B = 11, C = 2, X = 20;
cout << findCost(A, B, C, X) << " " ;
return 0;
}
|
Java
class GFG {
static final int MAX = 1000 ;
static int max_prime[] = new int [MAX];
static int min_prime[] = new int [MAX];
static void sieve( int n)
{
for ( int i = 2 ; i < n; ++i) {
if (min_prime[i] > 0 ) {
continue ;
}
min_prime[i] = i;
max_prime[i] = i;
int j = i + i;
while (j < n) {
if (min_prime[j] == 0 ) {
min_prime[j] = i;
}
max_prime[j] = i;
j += i;
}
}
}
static int findCost( int A, int B, int C, int X)
{
sieve(MAX);
int N, M;
if (X == 1 ) {
return - 1 ;
}
int min_cost = C + X;
int cost_for_prime = A;
N = max_prime[X];
if (N != X) {
M = X / N;
cost_for_prime += M;
min_cost = Math.min(min_cost, cost_for_prime);
}
M = min_prime[X];
N = X / M;
if (N != min_prime[N]) {
int cost_for_comp = B + M;
min_cost = Math.min(min_cost, cost_for_comp);
}
return min_cost;
}
public static void main (String[] args)
{
int A = 7 , B = 11 , C = 2 , X = 20 ;
System.out.println(findCost(A, B, C, X));
}
}
|
Python3
MAX = 10000 ;
max_prime = [ 0 ] * MAX ;
min_prime = [ 0 ] * MAX ;
def sieve(n) :
for i in range ( 2 , n) :
if (min_prime[i] > 0 ) :
continue ;
min_prime[i] = i;
max_prime[i] = i;
j = i + i;
while (j < n) :
if (min_prime[j] = = 0 ) :
min_prime[j] = i;
max_prime[j] = i;
j + = i;
def findCost(A, B, C, X) :
sieve( MAX );
if (X = = 1 ) :
return - 1 ;
min_cost = C + X;
cost_for_prime = A;
N = max_prime[X];
if (N ! = X) :
M = X / / N;
cost_for_prime + = M;
min_cost = min (min_cost, cost_for_prime);
M = min_prime[X];
N = X / / M;
if (N ! = min_prime[N]) :
cost_for_comp = B + M;
min_cost = min (min_cost, cost_for_comp);
return min_cost;
if __name__ = = "__main__" :
A = 7 ; B = 11 ; C = 2 ; X = 20 ;
print (findCost(A, B, C, X)) ;
|
C#
using System;
class GFG {
static int MAX = 1000;
static int []max_prime = new int [MAX];
static int []min_prime = new int [MAX];
static void sieve( int n)
{
for ( int i = 2; i < n; ++i) {
if (min_prime[i] > 0) {
continue ;
}
min_prime[i] = i;
max_prime[i] = i;
int j = i + i;
while (j < n) {
if (min_prime[j] == 0) {
min_prime[j] = i;
}
max_prime[j] = i;
j += i;
}
}
}
static int findCost( int A, int B, int C, int X)
{
sieve(MAX);
int N, M;
if (X == 1) {
return -1;
}
int min_cost = C + X;
int cost_for_prime = A;
N = max_prime[X];
if (N != X) {
M = X / N;
cost_for_prime += M;
min_cost = Math.Min(min_cost, cost_for_prime);
}
M = min_prime[X];
N = X / M;
if (N != min_prime[N]) {
int cost_for_comp = B + M;
min_cost = Math.Min(min_cost, cost_for_comp);
}
return min_cost;
}
public static void Main ( string [] args)
{
int A = 7, B = 11, C = 2, X = 20;
Console.WriteLine(findCost(A, B, C, X));
}
}
|
Javascript
<script>
let MAX = 1000;
let max_prime = Array.from({length: MAX}, (_, i) => 0);
let min_prime = Array.from({length: MAX}, (_, i) => 0);
function sieve(n)
{
for (let i = 2; i < n; ++i) {
if (min_prime[i] > 0) {
continue ;
}
min_prime[i] = i;
max_prime[i] = i;
let j = i + i;
while (j < n) {
if (min_prime[j] == 0) {
min_prime[j] = i;
}
max_prime[j] = i;
j += i;
}
}
}
function findCost(A, B, C, X)
{
sieve(MAX);
let N, M;
if (X == 1) {
return -1;
}
let min_cost = C + X;
let cost_for_prime = A;
N = max_prime[X];
if (N != X) {
M = X / N;
cost_for_prime += M;
min_cost = Math.min(min_cost, cost_for_prime);
}
M = min_prime[X];
N = X / M;
if (N != min_prime[N]) {
let cost_for_comp = B + M;
min_cost = Math.min(min_cost, cost_for_comp);
}
return min_cost;
}
let A = 7, B = 11, C = 2, X = 20;
document.write(findCost(A, B, C, X));
</script>
|
Time Complexity: O(MAX)2
Auxiliary Space: O(MAX)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
07 Mar, 2022
Like Article
Save Article