Instagram
youtube
Facebook
Twitter

Python programme to find the maximum possible revenue Codechef solution

Problem:

You are developing a smartphone app. You have a list of potential customers who are interested in buying 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 would want to declare a price such that the revenue you earn from the customers is maximised. Find the maximum possible revenue.

Example

Suppose you have four potential customers, and their budgets are Rs 30, Rs 20, Rs 53, and Rs 14. In this case, the maximum revenue you can get is 60 rupees.

How is 60 rupees the maximum revenue?

A revenue of Rs 60 is achieved by selling the app at either Rs 20 or Rs 30.

If we sell the app at $20, then only customers 1, 2, and 3 can buy it. 

But total revenue = 20*3 = 60. 

If we had sold the app at $14 (the lowest price), then all customers could have bought it. 

But total revenue would be 14 *4 = 56 (which is lower than 60).

Input format

Line 1: N is the total number of potential customers.
Lines 2 to N+1: Each line has the budget of a potential customer.

Output format

The output consists of a single integer, the maximum possible revenue you can earn from selling your app.

Sample Input 

4
30
20
53
14

Sample Output 

60

Solution:

n=int(input(“Enter the number of terms: “))
l=[]
for i in range(n):
    l.append(int(input(“Enter the budgets of the customers: “)))
l.sort()
lis=[]
for i in range(n):
    lis.append(l[i]*(n-i))
print(“Revenue generated: “,max(lis))

 

Steps to solve this problem:

  • Ask the user to enter the number of terms to execute the loop and store it in the variable n.

  • Take an empty list and append to it the budgets of the customers as provided by the user.

  • Using the sort() function, arrange the list in ascending order.

  • Take another empty list and Calculate revenue by multiplying (n-1) with each element of list l and appending it to list lis.

  • Using the max() function, get the maximum value from list lis and print it.