Data Structures & AlgorithmsVery hard

What is the maximum sum of a contiguous subarray of -3 4 -1 2 5 -6 3 1 whose LENGTH is between 2 and 3 inclusive?

  1. A. 10
  2. B. 6
  3. C. 7
  4. D. 15

Correct answer: C. 7

Slide windows of length 2 and 3 over prefix sums: the best is [2, 5] = 7 (length 3 peaks at 6 with [−1, 2, 5]). Plain Kadane ignores the cap and returns 4 − 1 + 2 + 5 = 10, 6 checks length 3 only, and 15 adds every positive element.

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

Exams that ask this topic