Data Structures & AlgorithmsHardCodeforces Div2 B · TCS NQT

Each operation decreases one element of [5, 3, 4, 2, 6] by exactly 1. What is the minimum number of operations needed to make the array non-decreasing?

  1. A. 7
  2. B. 6
  3. C. 4
  4. D. 18

Correct answer: B. 6

Scan from the right: each element may be lowered to the value of its right neighbour when it is larger, and every lowering is forced. Summing the descents of the original array counts only one step per drop and misses the cascades.

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

Exams that ask this topic