Data Structures & AlgorithmsVery hardLeetCode-style · Codeforces Div2 D
Choose a subsequence of [5, -2, 7, 1, -3, 6, 4] in which any two consecutive CHOSEN indices differ by at most 2 positions (by index). A subsequence may be any non-empty set of such positions. What is the maximum possible sum?
- A. 24
- B. 23
- C. 18
- D. 14
Correct answer: B. 23
dp[i] = a[i] + max(0, max of dp[j] for i − K ≤ j < i), taking the best chain that ends within the allowed gap. Restricting to consecutive positions (gap 1) gives a smaller total.
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 ways can "abcabcdaab" be cut into a non-empty left part and a non-empty right part so that the two parts contain the same number…
- How many DISTINCT values appear among the sums of all non-empty contiguous subarrays of [2, -1, 3, 0, -2, 4]?
- How many unordered pairs of positions (i < j) in "abababcabab" have the same 2-letter substring starting there (s[i:i+2] == s[j:j+2])?
- People stand in a row with heights [3, 1, 4, 2, 5, 2]. Two people can see each other if every person strictly between them is shorter than…
- How many contiguous subarrays of [3, 1, 4, 1, 5, 9, 2] have their FIRST element equal to their minimum (ties allowed)?
- A linked list holds the values [1, 2, 3, 4, 5, 6, 7, 8]. Reverse every complete group of 3 consecutive nodes; a final group with fewer…