Data Structures & AlgorithmsHardGATE CS · TCS NQT

What is the maximum value of a[i] + a[j] over pairs with j - i >= 2 in [5, 1, 8, 2, 9, 3]?

  1. A. 18
  2. B. 17
  3. C. 14
  4. D. 12

Correct answer: B. 17

Keep the prefix maximum of a[0..j−2] and add a[j]: 8 (index 2) + 9 (index 4) = 17. Pairing 9 with itself gives 18, demanding a gap of 3 gives 14, and 12 is 9 plus its adjacent neighbour 3.

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

Exams that ask this topic