Problem · Heap

Get Maximum Amount

Learn this problem
MediumIBMFULLTIMEOA
See IBM hiring insights

Problem statement

There are n types of items in a shop, where the number of items of type i is denoted by quantity[i]. The price of the items is determined dynamically, where the price of the ith item is equal to the remaining number of items of type i. There are m customers in line to buy the items from the shop, and each customer will buy exactly one item of any type.

The shopkeeper, being greedy, tries to sell the items in a way that maximises revenue. Find the maximum amount the shopkeeper can earn by selling exactly m items to the customers optimally.

Function

getMaximumAmount(quantity: int[], m: int) → long

Complete the function getMaximumAmount in the editor.

getMaximumAmount has the following parameter:

  1. int quantity[n]: the number of items of each type

Returns

long integer: the maximum revenue possible

Examples

Example 1

quantity = [10, 10, 8, 9, 1]m = 6return = 55
N/A for now

Example 2

quantity = [1, 2, 4]m = 4return = 11
Example 2 illustration

Constraints

  • 1 ≤ n ≤ 10^5
  • 1 ≤ m ≤ 10^5
  • 1 ≤ quantity[i] ≤ 10^5

More IBM problems

drafts saved locally
public long getMaximumAmount(int[] quantity, int m) {
  // write your code here
}
quantity[10, 10, 8, 9, 1]
m6
expected55
checking account