Data Structures & AlgorithmsHard

A window with at most K replacements (or at most K zeros) is found with two pointers. Why is it safe for the left pointer to only move forward?

  1. A. Left pointers must alternate direction to find the best answer.
  2. B. Extending the window to the right can only make the count of violations larger or equal, so an invalid left end never becomes valid again.
  3. C. The right pointer always moves at double the speed of the left.
  4. D. Each window is independent, so the left pointer may reset to zero.

Correct answer: B. Extending the window to the right can only make the count of violations larger or equal, so an invalid left end never becomes valid again.

Violations are monotone in the window's right end, so once a left end is too far left it stays too far left. The left pointer therefore sweeps forward once and the whole process is linear.

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

Exams that ask this topic