Data Structures & AlgorithmsVery hardCodeforces Div2 D · TCS NQT
Choose a subsequence of [4, 7, 2, 9, 5, 6, 1] with no two chosen positions adjacent, such that its total is ODD. What is the largest such total?
- A. 17
- B. 23
- C. 19
- D. 20
Correct answer: C. 19
Use a DP with a parity state: taking index i forces skipping i+1, and the parity of the running total is tracked alongside the best sum. Taking the maximum non-adjacent sum and then fixing parity can lose the optimal odd sum.
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
- Count the pairs of positions (i < j) in [3, 4, 7, 2, 9, 6] such that a[i] + a[j] is odd and i + j is even.
- Choose two positions i < j with j − i ≥ 3 in [5, -4, 3, 8, -6, 2, 7]. What is the largest possible product a[i] · a[j]?
- What is the minimum number of characters you must delete from "aaabbbbcccccdd" so that no three equal letters are consecutive?
- How many non-empty subsets of {1, 2, ..., 8} have a sum divisible by 4?
- How many contiguous subarrays of [3, 1, 4, 1, 5, 2] have their FIRST element as their maximum (ties count as maximum)?
- What is the length of the longest subsequence (any positions, keeping order) of [2, 3, 5, 1, 4] whose product is at most 12?