Data Structures & AlgorithmsVery hardProduct company OA

What is the maximum sum of a non-empty contiguous subarray of the CIRCULAR array [5, −8, 3, 4, −2, 6, −9, 7] (a subarray may wrap from the end to the start)?

  1. A. 15
  2. B. 11
  3. C. 12
  4. D. 14

Correct answer: A. 15

Linear Kadane gives 3 + 4 − 2 + 6 = 11. The wrap-around best is total − (minimum subarray): total = 6 and the minimum subarray is just [−9], so 6 − (−9) = 15, i.e. every element except −9: 7, 5, −8, 3, 4, −2, 6. Answer max(11, 15) = 15. '12' takes only 7 + 5; '11' ignores wrapping.

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

Exams that ask this topic