Data Structures & AlgorithmsHardCodeforces Div2 D · GATE CS

Count the index paths from position 0 to position 6 in the array [1, 3, 2, 4, 6, 5, 7] where each move goes from index i to a LATER index j with 1 ≤ a[j] − a[i] ≤ 2. How many such paths exist?

  1. A. 8
  2. B. 0
  3. C. 5
  4. D. 13

Correct answer: C. 5

A DAG path-count DP: cnt[j] = Σ cnt[i] over earlier i with the value difference in [1, d]. Counting only the adjacent moves (j = i + 1) is a much smaller set of paths.

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

Exams that ask this topic