Data Structures & AlgorithmsHard

Treat 5 -2 3 4 -6 1 as a CIRCULAR array. What is the largest sum of three consecutive (circular) elements?

  1. A. 7
  2. B. 13
  3. C. 12
  4. D. 6

Correct answer: D. 6

Wrap indices modulo n and slide a window of 3: sums are 6, 5, 1, −1, 0, 4, so the best is 6 (5 − 2 + 3), and the wrapping windows happen to be smaller here. A window of 2 gives 3 + 4 = 7, 13 adds all positives, and 12 picks the three largest values regardless of position.

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

Exams that ask this topic