Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT

Children stand in a row with ratings [1, 2, 2, 5, 4, 3, 3, 8, 1]. Each child gets at least one candy, and a child with a higher rating than a neighbour gets more candies than that neighbour. What is the minimum total number of candies?

  1. A. 12
  2. B. 14
  3. C. 29
  4. D. 15

Correct answer: B. 14

Two passes are needed: a left-to-right pass fixes rises from the left neighbour, and a right-to-left pass fixes the right neighbour while keeping the larger requirement. A single pass misses the case where a descent should raise the earlier child's count.

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

Exams that ask this topic