GATE Computer ScienceHardGATE CS (PYQ pattern)

What is the maximum product of a non-empty contiguous subarray of [−2, 3, −4, 0, 5, −1, 2, −3, −1, 4]?

  1. A. 24
  2. B. 40
  3. C. 120
  4. D. 30

Correct answer: D. 30

The zero splits the array. In the left part −2·3·(−4) = 24; in the right part the best window is 5·(−1)·2·(−3) = 30, since extending it by −1 gives −30 and then by 4 gives −120. Tracking both the maximum and the minimum running product finds 30.

More Algorithms questions

Exams that ask this topic