Data Structures & AlgorithmsModerate

For the array [4, -1, 2, -3, 5, -6, 3], what is the maximum value of (sum of a non-empty contiguous subarray) − 2 × (its length)?

  1. A. 7
  2. B. 18
  3. C. 3
  4. D. 5

Correct answer: C. 3

Subtracting K from every element and then running Kadane's algorithm gives sum(b) − K·len(b), because each element of a subarray contributes one copy of K. Ignoring K, adding it, or subtracting it only once from the whole subarray gives a different maximum.

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

Exams that ask this topic