Data Structures & AlgorithmsVery hardCAT · Codeforces Div2 B

How many index pairs i < j in the array [6, 9, 2, 7, 4, 1] satisfy a[i] + i = a[j] + j (0-based indices)?

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

Correct answer: D. 2

Keys a[i] + i are equal for both indices of a pair, so group by the key and add the pair counts. Using a[i] - i instead of a[i] + i changes the grouping.

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

Exams that ask this topic