-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMaximum Profit on App
More file actions
70 lines (62 loc) · 1.75 KB
/
Maximum Profit on App
File metadata and controls
70 lines (62 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
Maximum Profit on App
Send Feedback
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
where m is total number of subscribers whose budget is greater than or equal to x.
Input format :
Line 1 : N (No. of subscribers)
Line 2 : Budget of subscribers (separated by space)
Output Format :
Maximum profit
Constraints :
1 <= N <= 10^6
1 <=budget[i]<=9999
Sample Input 1 :
4
30 20 53 14
Sample Output 1 :
60
Sample Output 1 Explanation :
Price of your app should be Rs. 20 or Rs. 30. For both prices, you can get the profit Rs. 60.
Sample Input 2 :
5
34 78 90 15 67
Sample Output 2 :
201
code in java ***********************************************
import java.util.Scanner;
import java.util.Arrays;
public class solution
{
public static int maximumProfit (int budget[])
{
Arrays.sort (budget);
int[] profit = new int[budget.length];
for (int i = 0; i < budget.length; i++)
{
profit[i] = (budget.length - i) * budget[i];
}
int max = Integer.MIN_VALUE;
for (int i:profit)
{
if (max < i)
{
max = i;
}
}
return max;
}
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int n = sc.nextInt ();
int[] arr = new int[n];
for (int i = 0; i < n; i++)
{
arr[i] = sc.nextInt ();
}
System.out.print (maximumProfit (arr));
}
}