Data Structures & AlgorithmsVery hard

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

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

Correct answer: A. 5

Test every index pair for an absolute difference of exactly one; equal values and gaps of two or more fail. Allowing zero differences, counting ordered differences only, or counting distinct values answers different questions.

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

Exams that ask this topic