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?

  1. A. 24
  2. B. 23
  3. C. 18
  4. 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.

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

Exams that ask this topic