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?
- A. 17
- B. 21
- C. 15
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- A circular list holds 1..10. Starting at 1, you count 4 nodes (including the current one) and delete the fourth, then continue counting…
- Buildings of heights [3, 7, 8, 3, 6, 1] are viewed from the left. How many buildings are visible (a building is visible if it is strictly…
- Evaluate the postfix expression "3 4 min 5 max 2 +" where min and max take two operands and return the smaller or larger.
- What is the length of the longest contiguous subarray of [2, 0, 0, 2, 0, 2, 2] whose sum is 0?
- How many integers from 1 to 100 inclusive are divisible by none of 2, 3 and 5?
- What is the digital root of the product 9 18 27 34?