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)?
- A. 4
- B. 8
- C. 6
- 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.
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
- How many contiguous subarrays of [1, 2, 3, 4, 7, 10, 13, 14, 15, 16] are arithmetic (at least 3 elements, with a constant difference…
- What is the length of the longest contiguous substring of the binary string "1100100110111000101" that has equally many 0s and 1s?
- What is the smallest positive period p of the string abcabcabca, meaning the smallest p such that s[i] = s[i+p] for every valid i?
- How many distinct cyclic rotations does the string abcabc have (rotations are the strings obtained by moving a prefix to the end…
- How many subarrays of [1, 2, 1, 2, 2, 1] read the same forwards and backwards (as sequences of values)?
- What is the length of the longest palindromic substring of xabacdcabay?