Data Structures & AlgorithmsHardCodeforces Div2 C · GATE CS

A shop has a 'buy 2, get the cheapest of the three free' deal. Items priced [3, 1, 4, 2, 5, 6] are bought in groups of three, with the cheapest item in each group free, and the groups are formed greedily after sorting by price descending. What is the total cost paid?

  1. A. 17
  2. B. 21
  3. C. 15
  4. D. 16

Correct answer: D. 16

Sort descending and form groups of three, paying for the two most expensive items in each group. Grouping the most expensive items together is the greedy that maximises the free item's value.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic