Data Structures & AlgorithmsHardGATE CS · ICPC regional

How many index pairs i < j satisfy a[i] - i = a[j] - j for the array [7, 3, 5, 8, 6, 9, 4, 10] (0-based indices)?

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

Correct answer: A. 2

Rewriting the condition as a[i] - i = a[j] - j turns it into counting equal keys in a hash map of shifted values, which runs in O(n).

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

Exams that ask this topic