Data Structures & AlgorithmsVery hardCodeforces Div2 D · TCS NQT

Choose a subsequence of [3, 8, 1, 9, 2, 7] (keeping order, possibly any length ≥ 1). Its alternating sum is the first chosen element minus the second, plus the third, minus the fourth, and so on. What is the maximum alternating sum?

  1. A. 19
  2. B. 9
  3. C. 30
  4. D. 21

Correct answer: D. 21

Two states while scanning: the best value with an odd number of picked elements (next pick adds) and with an even number (next pick subtracts). Taking the plain maximum subsequence sum ignores the sign pattern.

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

Exams that ask this topic