Data Structures & AlgorithmsVery hardICPC regional · Amazon SDE OA

How many index triples i < j < k in [1, 2, 3, 4, 5, 6] satisfy a[i] + a[k] = 2 * a[j]?

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

Correct answer: C. 6

a[i] + a[k] = 2a[j] means the three values form an arithmetic progression: difference 1 gives (1,2,3), (2,3,4), (3,4,5), (4,5,6), difference 2 gives (1,3,5), (2,4,6), so 6. Only difference 1 gives 4, only difference 2 gives 2, and 20 counts all triples.

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

Exams that ask this topic