ProgrammingVery hardTCS NQT · Accenture

Array a = [5, -3, 5] is treated as circular. The maximum circular subarray sum (a contiguous block that may wrap around the end) is found by taking the larger of the best ordinary subarray sum and (total sum minus the smallest ordinary subarray sum). What is printed?

Array a = [5, -3, 5] is treated as circular. The maximum circular subarray sum (a contiguous block that may wrap around the end) is found by taking the larger of the best ordinary subarray sum and (total sum minus the smallest ordinary subarray sum). What is printed?
ordinary max = Kadane max; ordinary min = Kadane min
total = sum of a
Print max(ordinary max, total - ordinary min)
  1. A. 7
  2. B. 5
  3. C. 10
  4. D. 2

Correct answer: C. 10

The ordinary best is 7 (5, -3, 5), but wrapping gives 5 + 5 = 10 after removing the middle -3, which is the answer. A non-circular Kadane gives 7, the largest element gives 5, and adding the largest and smallest elements gives 2.

More Pseudocode and Code Tracing questions

Exams that ask this topic