-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxmProfitOnApp.cpp
69 lines (66 loc) · 2.02 KB
/
maxmProfitOnApp.cpp
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
/*
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
Sample Output 2 Explanation :
Price of your app should be Rs. 67. You can get the profit Rs. 201 (i.e. 3 * 67).
*/
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int maximumProfit(int budget[], int n) {
// Write your code here
// sort(budget, budget + n);
// int maxProfit[n];
// int i = n;
// for(; i > 0; i--){
// maxProfit[i] = budget[i] * i;
// }
// // int max = maxProfit[0];
// // for(int j = 0; j < n; j++){
// // if(max < maxProfit[i])
// // max = maxProfit[i];
// // }
// // return max;
// sort(maxProfit, maxProfit + n);
// return maxProfit[n];
int ans=INT_MIN;
sort(budget,budget+n);
for(int i=0;i<n;i++)
{
ans=max(ans,budget[i]*(n-i));
}
return ans;
}
int main() {
int n ,*input,i,*cost;
cin>>n;
input = new int[n];
for(i = 0;i < n;i++)
cin>>input[i];
cout << maximumProfit(input, n) << endl;
}