Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT

What is the maximum sum of a contiguous subarray of [4, -1, 2, 2, 5, -3, 4, 1] that contains at most 2 distinct values?

  1. A. 5
  2. B. 9
  3. C. 10
  4. D. 8

Correct answer: B. 9

Use a sliding window with a count map of distinct values; the window sum is maintained as it grows and shrinks. The Kadane sum ignores the distinct-value limit and can take in too many distinct values.

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

Exams that ask this topic