-1

You are developing a smartphone app. You have a list of potential customers for your app. Each customer has a budget and will buy the app at your declared price if and only if the price is less than or equal to the customer's budget.

You want to fix a price so that the revenue you earn from the app is maximized. Find this maximum possible revenue.

For instance, suppose you have 4 potential customers and their budgets are 30, 20, 53 and 14. In this case, the maximum revenue you can get is 60.

my friend told me that just sort the array and try using

ar[i]*(n-i) although i implemented i didnt under stood the whole logic.really need help with the explanation

#include<bits/stdc++.h>
using namespace std;

int maximumProfit(int budget[], int 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;
}
yolon bsn
  • 81
  • 1
  • 4
  • 1
    Unrelated: Be really cautious with `#include` and `using namespace std;` The first includes the entire C++ Standard library. That's a tens of thousands of identifiers you aren't using that you now have to avoid resusing. Many of these identifiers you probably don't even know exist. I don't. `using namespace std;` effectively pulls the `std` namespace into the global namespace where accidental reuse is much more likely. – user4581301 Oct 07 '19 at 18:11
  • Related: once data is sorted, why look for the max or min? Unless the sorting criteria is really weird, and this code is using the default criteria, the minimum value will be at one end of the data set and the maximum value will be at the other end. Makes them quite easy to find. Unless you have supplied custom `sort` or custom `max` functions. In this case that first comment becomes much, much more relevant. – user4581301 Oct 07 '19 at 18:13
  • @user4581301 code is trying the find the max of `budget[i] * (n - i)` which is different from `budget[i]`. – Mutable Side Effect Oct 07 '19 at 18:16
  • How to solve the problem: Put the code away. Work out the logic with pen and paper. How would you solve this problem with your mark I brain? Take notes on your process. These notes will become the steps you must instruct the computer to perform. – user4581301 Oct 07 '19 at 18:17

3 Answers3

1

Total Customers N = 4

(sorted) budget_list = [14, 20, 30, 53]

Fixed price = 14, all our customers buy our product

Revenue = 14 * 4 = 56, Given by budget_list[0] * (N)

Next price = 20, three customers buy the app(i.e expect the customer with budget 14)

Revenue = 20 * 3, Given by budget_list[1] * (N-1)

So the maximum revenue is can be obtained by iterating through the budget_list for range of customers(N)

0

Intuition: Let's say that the poorest person in the optimal solution has a budget of x. Then you can always choose x as the price because everyone else is at least as rich and choosing a lower price will only reduce your revenue (unless he is not the poorest). After realizing this, you're simply iterating over the customers to find the person who is the poorest in the optimal solution (there can be multiple but then you choose all of them). The total revenue is the budget of the candidate times the number of people who are at least as rich, which is budget[i] * (n - i) since the budgets are sorted in non-decreasing order.

0

must use long long in place of int

#include <iostream>
#include<bits/stdc++.h>

using namespace std;

long long maxp(long long arr[], long long n){
sort(arr, arr+n);
long long ans = arr[0];
for(long long i=0; i<n; i++){
    ans=max(ans,arr[i]*(n-i));
}
cout<<ans;
}

int main() {
long long n;
cin>>n;
long long arr[n];
for(long long i=0; i<n; i++){
    cin>>arr[i];
}
maxp(arr, n);
return 0;
}