Data Structures & AlgorithmsHardCodeforces Div2 C · GATE CS

What is the length of the longest subsequence of [1, 2, 4, 5, 7, 8, 10] that is strictly increasing and in which every two consecutive chosen values differ by at least 2?

  1. A. 5
  2. B. 3
  3. C. 6
  4. D. 4

Correct answer: D. 4

dp[i] = 1 + max dp[j] over earlier j with a[i] − a[j] ≥ 2 (the strict increase is implied). Chaining with only a strict increase, without the gap condition, admits consecutive values such as 4 and 5.

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

Exams that ask this topic