Data Structures & AlgorithmsHard
Treat 5 -2 3 4 -6 1 as a CIRCULAR array. What is the largest sum of three consecutive (circular) elements?
- A. 7
- B. 13
- C. 12
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- How many index pairs i < j in 1 4 1 4 4 2 1 2 2 hold EQUAL values and satisfy j − i ≤ 2?
- How many index triples i < k < j in 2 4 6 3 9 5 satisfy A[i] + A[j] = 2·A[k] (the middle value is the arithmetic mean of the outer two)?
- What is the length of the longest contiguous subarray of 1 2 3 5 1 2 3 such that the sum of EVERY prefix of that subarray is divisible by 3?
- Why is a stack-based check that tracks TYPE as well as nesting needed for two bracket kinds?
- To count distinct subsequences of a string with repeated letters, which strategy avoids double counting?
- For the 'largest number from concatenation' problem, why does the comparator compare x+y with y+x as strings?