Customer and Discount

4.5

8 votes
Binary Search, , Algorithms, Greedy Algorithms
Problem

You are given an array arr of length N, representing money with N customers. The i-th customer has arri rupees. There is another array cost of length M, representing the cost of M items in a store. The j-th item costs costj rupees. The store offers some discount money d which any customer can use to buy an item. Each customer can buy only one item and cannot use his money to buy an item for any other customer.

Task

Determine the maximum number of customers who can buy an item and the minimum total sum of customers’ money spent to achieve maximum customers with an item.

Notes

  • Assume 1-Based Indexing.
  • The discount money d is common for all the customers, i.e., if one customer spends r rupees from d, then the discount money is reduced to (d-r) for all the customers.
  • Multiple customers can spend money from the discount.

Example

Assumptions

  • N = 3
  • M = 2
  • d = 100
  • arr = [30, 40, 50]
  • cost = [80, 110]

Approach

Initially, we have discount money equals 100.

  • 2nd customer buys 1st item worth 80 rupees using 40 rupees of discount money.
  • Now the discount money left is 100 - 40 = 60.
  • 3rd customer buys 2nd item worth 110 rupees using 60 rupees of discount money.
  • The total number of customers who can buy an item is 2 with their total sum of money spent equals 90.

Therefore the answer is [2 90].

Function Description

Complete the function maxCustomers provided in the editor. The function takes the following parameters and returns an array consisting of 2 integers, the maximum number of customers with items, and the minimum total customers' money spent:

  • N: Represents the size of array arr
  • M: Represents the size of array cost
  • d: Represents the value of discount money
  • arr: Represents the elements of array arr
  • cost: Represents the elements of array cost

Input format

Note: This is the input format you must use to provide custom input (available above the Compile and Test button).

  • The first line of the input contains three integers N, M, and d, denoting the number of customers, the number of items, and the discount money.
  • The second line of the input contains N integers arr1, arr2, …, arrN, where arri is the money with the i-th customer.
  • The third line of the input contains M integers cost1, cost2, …, costM, where costj is the price of the j-th item.

Output format

Print two space-separated numbers, the maximum number of customers who can buy an item and the minimum total customers' money needed for buying the maximum number of items.

Constraints

1N105
1M105
0d109
1arri104  i[1,N]
1costj109  j[1,M]

Code snippets (also called starter code/boilerplate code)

This question has code snippets for C, CPP, Java, and Python.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Given

  • N = 2
  • M = 2
  • d = 50
  • arr = [20, 30]
  • cost = [50, 30]

Approach

Initially we have discount money equals 50.

  • 1st customer buys 1st item worth 50 rupees by using 30 rupees from discount money.
  • Now discount money left is 50-30 = 20.
  • 2nd customer buys 2nd item worth 30 rupees by using remaining 20 rupees of discount money.
  • The total number of customers who can buy an item is 2 with their total sum of money spent equals to 30.

Therefore the answer is [2 30].

Editor Image

?