Data Structures & AlgorithmsVery hardCodeforces Div2 C

For the array [3, -7, 4, -2, 5, -9, 6, 1], you may delete at most ONE element (the rest keep their order). Then you take a non-empty contiguous subarray of what remains. What is the largest possible sum?

  1. A. 14
  2. B. 7
  3. C. -2
  4. D. 13

Correct answer: A. 14

Track two states while scanning: the best sum ending here with no deletion used, and the best sum ending here where exactly one element has been dropped. Plain Kadane ignores the deletion, and skipping the state that starts right after the deleted element misses the best segment.

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

Exams that ask this topic