ProgrammingHardInfosys · Cognizant

Array a = [3, 6, -2, -5, 7, 3]. The procedure below finds the largest product of two adjacent elements. What is printed?

Array a = [3, 6, -2, -5, 7, 3]. The procedure below finds the largest product of two adjacent elements. What is printed?
best = a[0] * a[1]
for i = 1 to n - 2
    best = max(best, a[i] * a[i+1])
end for
Print best
  1. A. -35
  2. B. 10
  3. C. 21
  4. D. 18

Correct answer: C. 21

Adjacent products are 18, -12, 10, -35, 21, so the largest is 21 (from 7 and 3). The smallest product is -35, the largest sum of neighbours is 10, and comparing with the previous neighbour gives 18.

More Pseudocode and Code Tracing questions

Exams that ask this topic