Data Structures & AlgorithmsVery hard

How many index pairs i < j in 1 3 2 5 4 6 8 satisfy A[j] − A[i] = j − i?

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

Correct answer: A. 5

Rewrite the condition as A[j] − j = A[i] − i, so pairs are counted among equal values of the shifted array B[k] = A[k] − k. Doubling the slope, reversing the difference, or shifting by one each give other counts.

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

Exams that ask this topic