FastPrepFastPrep
Problem Brief

Items Purchase (For Java Backend Engineer)

OA

n items where the price of the ith item is price[i]. A frequent customer has m discount coupons. If x discount coupons are used on the ith item, its price is reduced to the integer floor(price(i) / 2^x), e.g. floor(3/2^1) = floor(1.5) = 1. Find the minimum amount needed to purchase all the items of the shop using at most m coupons.

Function Description

Complete the function findMinimumPrice in the editor below.

findMinimumPrice has the following parameters:

  1. int price[n]: the original prices of the items
  2. int m: the number of discount coupons

Returns

int: the minimum amount needed to purchase all items

1Example 1

Input
price = [2, 4], m = 2
Output
3
Explanation

The optimum solution:

  • Purchase item 1 for 2.
  • Use 2 coupons on item 2, so the discounted price is 4/2^2 = 4 / 4 = 1.
The amount required = 2 + 1 = 3.

Constraints

Limits and guarantees your solution can rely on.

  • 1 <= n <= 10^5
  • 0 <= m <= 10^9
  • 1 <= price[i] <= 10^9
  • public int findMinimumPrice(int[] price, int m) {
      // write your code here
    }
    
    Input

    price

    [2, 4]

    m

    2

    Output

    3

    Sign in to submit your solution.