The grocery store is having a store-wide sale: buy n items, get 1 item free. The free item must be of equal or lesser value than each of the n items. Fortunately, the store checkout will always find the best deal for the shopper. No matter what order the items are purchased in, it will always choose the highest-priced free item(s) that meet the criteria.
Write a function that calculates the total paid by the shopper, given the number of items to buy before getting 1 free, and a vector giving the price of each item purchased.
For example, if n = 4, and prices = [4.49 4.49 5.19 4.79 4.79], then total = 19.26, since the qualifying free item must be one of the items priced at $4.49.
If n = 2, and prices = [1 2 3 4 5 6 7], then total = 21. Buying the items priced $6 and $7 gets you the item priced $5 for free, and buying the items priced $3 and $4 gets you the item priced $2 for free. The item priced $1 is not part of any deal.
Solution Stats
Problem Comments
2 Comments
Solution Comments
Show comments
Loading...
Problem Recent Solvers4
Suggested Problems
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Nice problem, Anne!
I agree. This is a good problem.