Question 1 - Maximum Revenue from Suppliers
Amazon is hosting a flash sale for a high-demand product sourced from multiple suppliers. Each supplier has a limited stock, represented by an array supplierStock, where each element indicates the number of units available from that supplier.
To maximize revenue, Amazon follows a dynamic pricing strategy:
Rules
At any given time, only one unit can be sold from a supplier.
The revenue from selling a unit equals the supplier’s current stock level at that moment.
After a sale, the supplier’s stock decreases by 1, and the price updates accordingly.
If a supplier’s stock reaches zero, no further sales can be made from that supplier.
Amazon must sell exactly orders items and wants to maximize total revenue.
Problem Statement
Given:
An integer array supplierStock of length n, representing stock levels across suppliers.
A long integer orders, representing the total number of items Amazon needs to sell.
Determine the maximum revenue that can be generated.
Function Description
Complete the function getMaxRevenue.
Parameters
int supplierStock[n] Array where each element represents the initial stock of a supplier.
long int orders Total number of items Amazon needs to sell.
Returns
long int — maximum revenue achievable.
Constraints
1 ≤ n ≤ 10^5
1 ≤ supplierStock[i] ≤ 10^5
1 ≤ orders ≤ sum(supplierStock)
Input Format (Custom Testing)
First line: integer n (size of supplierStock)
Next n lines: each contains supplierStock[i]
Last line: long integer orders
Sample Case 0
Input
n = 2
supplierStock = [2, 5]
orders = 4
Output
14
Explanation
Optimal selling strategy:
Sell 1 unit from supplier with stock 5 → Revenue = 5
Sell 1 unit from same supplier (stock 4) → Revenue = 4
Sell 1 unit from same supplier (stock 3) → Revenue = 3
Sell 1 unit from supplier with stock 2 → Revenue = 2
Remaining stock: [1, 2]
Total revenue:
5 + 4 + 3 + 2 = 14
Hence, the answer is 14.
Example
Input
supplierStock = [3, 5]
orders = 6
Optimal Selling Strategy
Sell from supplier with stock 5 → Revenue = 5
Sell from same supplier → Revenue = 4
Sell from supplier with stock 3 → Revenue = 3
Sell from supplier with stock 3 → Revenue = 3
Sell from supplier with stock 2 → Revenue = 2
Sell from supplier with stock 2 → Revenue = 2
Remaining stock: [1, 1]
Total Revenue
5 + 4 + (2 × 3) + (2 × 2) = 19
Hence, the answer is 19.
Sample Case 1
Input
n = 5
supplierStock = [2, 8, 4, 10, 6]
orders = 20
Output
110
Explanation
Amazon sells from suppliers until each has more than 2 units left.
Supplier 2: 8 + 7 + 6 + 5 + 4 + 3 = 33 (orders = 6)
Supplier 3: 4 + 3 = 7 (orders = 2)
Supplier 4: 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 = 52 (orders = 8)
Supplier 5: 6 + 5 + 4 + 3 = 18 (orders = 4)
Remaining stock after 20 orders:
[2, 2, 2, 2, 2]
Total Revenue
33 + 7 + 52 + 18 = 110
Hence, the answer is 110.