Wipro Coding Question 1
Minimum Cost to Buy N Apples
Here, in this page we will discuss the Wipro coding question asked in previous recruitment drive. We will write an efficient algorithm for the given question to find the minimum cost to buy N apples and there solution in C++ and Java.
Question 1
Josh went to the market to buy N apples. He found two shops, shop A and B, where apples were being sold in lots. He can buy any number of the complete lot(s) but not loose apples. He is confused with the price and wants you to figure out the minimum cost to buy exactly N apples. Write an algorithm for Josh to calculate the minimum cost to buy exactly N apples.
Input Format:
- The first line of the input consists of an integer – N, representing the total number of apples that Josh wants to buy.
- The second line consists of two space-separated positive integers – M1 and P1, representing the number of apples in a lot and the lot’s price at shop A, respectively.
- The third line consists of two space-separated positive integers-M2 and P2, representing the number of apples in a lot and lot’s price at shop B, respectively.
Output Format:
Print a positive integer representing the minimum price at which Josh can buy the apples.
Sample Input:
19
3 10
4 15
Sample Output:
65
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, M1, P1, M2, P2;
cin >> n >> M1 >> P1 >> M2 >> P2;
int mini = INT_MAX;
for (int i=0; M1*i <= n; i++)
{
int rem = n - i*M1;
if (rem%M2 == 0)
{
int cost = P1 * i + P2 * (rem/M2);
mini = (cost < mini or mini == -1) ? cost : mini;
}
}
if (mini != -1)
cout << mini;
else
cout << "Invalid inputs";
}
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m1=sc.nextInt();
int p1=sc.nextInt();
int m2=sc.nextInt();
int p2=sc.nextInt();
int min=Integer.MAX_VALUE;
for(int i=0;m1*i<=n;i++)
{
int rem=n-i*m1;
if(rem%m2==0)
{
int cost=p1*i+p2*(rem/m2);
min=(cost<min || min==-1)?cost:min;
}
}
if(min!=-1)
System.out.println(min);
}
}