Data Structures & AlgorithmsHard

In the sorted array 1 1 1 2 2 3 3 3 3 4, remove the extra copies so that every value appears AT MOST TWICE, keeping the order. What is the length of the resulting array?

  1. A. 7
  2. B. 4
  3. C. 8
  4. D. 6

Correct answer: A. 7

Two-pointer compaction: keep a new element only if it differs from the element two places behind it in the output. Counting distinct values or subtracting a fixed amount ignores how many copies each value has.

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

Exams that ask this topic