Data Structures & AlgorithmsHard

For the array [1, -2, 3, -1, 4, -6, 2], what is the maximum possible sum of a contiguous non-empty subarray after deleting AT MOST one of its elements (the remaining part must be non-empty)?

  1. A. 6
  2. B. 4
  3. C. 8
  4. D. 1

Correct answer: C. 8

Track the best sum with no deletion and with one deletion: deleting −6 from [3, −1, 4, −6, 2] gives 3 − 1 + 4 + 2 = 8. Plain Kadane (no deletion) gives 6, the largest element is 4 and the whole-array sum is 1.

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

Exams that ask this topic