Data Structures & AlgorithmsHardTCS NQT · Product Company OA

Problem: What is the maximum value of a[i] + a[j] - (j - i) over pairs i < j of a = [1, 3, -2, 5]? Run the code.

Problem: What is the maximum value of a[i] + a[j] - (j - i) over pairs i < j of a = [1, 3, -2, 5]? Run the code.
a = [1, 3, -2, 5]
print(max(a[i] + a[j] - (j - i) for i in range(len(a)) for j in range(i + 1, len(a))))
What is printed?
  1. A. 3
  2. B. 2
  3. C. 6
  4. D. 5

Correct answer: C. 6

Check every pair's score a[i] + a[j] − (j − i): (3, 5) at indices 1 and 3 gives 8 − 2 = 6, beating the adjacent pair (1, 3) and (1, 5), both 3. Charging the number of elements (j − i + 1) instead of the gap gives the tempting 5.

More Coding Interview Problems questions

Exams that ask this topic