Question: You have made a smartphone app and want to set its subscription price such that the profit earned is maximised.
There are certain users who will subscribe to your app only if their budget is greater than or equal to your price.
You will be provided with a list of size N having budgets of subscribers and you need to return the maximum profit that you can earn.
Lets say you decide that price of your app is Rs. x and there are N number of subscribers. So maximum profit you
can earn is :
m*x
Sample input 1:
30 20 53 14
Output 60
import java.util.*;
import java.util.Scanner;
public class solution {
public static int maximumProfit(int budget[]) {
Arrays.sort(budget);
// int maxProfit = 0;
// // Iterate through each possible subscription price and calculate profit
// for (int i = 0; i < budget.length; i++) {
// int currentProfit = budget[i] * (budget.length - i);
// maxProfit = Math.max(maxProfit, currentProfit);
// }
// return maxProfit;
// int currentProfit = 0;
int maxProfit = Integer.MIN_VALUE;
for(int i = 0; i < budget.length;i++){
int currentProfit = budget[i] * (budget.length-i);
if(currentProfit > maxProfit){
maxProfit = currentProfit;
}
}
return maxProfit;
}
}
Comments
Post a Comment