Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT

What is the maximum sum of a contiguous subarray of [3, -2, 7, 1, -5, 4, 8, -1, 2] whose length is between 2 and 4 inclusive?

  1. A. 13
  2. B. 9
  3. C. 12
  4. D. 14

Correct answer: A. 13

Use prefix sums and, for each end j, consider starts i with L ≤ j − i ≤ R, which is a sliding range of prefix values. Plain Kadane ignores the length bounds and can return a sum from a single element.

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

Exams that ask this topic