#352 Calculating Total Customer Dissatisfaction
Avito contains many products, each represented by a number. Every customer has a product need, also represented by a number. If there is no exact product, the customer chooses the product with the closest value, causing dissatisfaction equal to the difference between the need and the purchased product. The quantity of every product is unlimited, and the same product may be purchased by several customers.
Calculate the total dissatisfaction of all customers.
Write a function that accepts two arrays: an array of products and an array of customer needs, calculates the total dissatisfaction of all customers, and returns the result as a number.
# Example
# input
goods = [8, 3, 5]
buyerNeeds = [5, 6]
# output
res = 1 # the first customer buys product 5 and has dissatisfaction 0; the second also buys product 5 and has dissatisfaction 6-5 = 1
Question review
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor