Data Structures & AlgorithmsVery hardCodeforces Div2 C

What is the length of the longest subsequence of [1, 7, 4, 9, 2, 5, 3, 8] whose consecutive differences alternate in sign (each step goes strictly up then strictly down, or vice versa)?

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

Correct answer: B. 8

Track two DP values per index: the longest alternating subsequence ending here with the last step up, and with the last step down. Taking the LIS (all steps up) is not allowed, and neither is mixing steps of the same sign.

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

Exams that ask this topic