Data Structures & AlgorithmsVery hard

Why does the sliding-window approach fail to find the shortest subarray with sum divisible by m when values are NEGATIVE?

  1. A. Negative values break the monotonicity of window sums, so shrinking the window cannot guarantee the residue moves toward zero; prefix residues with a hash table are used instead.
  2. B. Negative numbers cannot be divided by m.
  3. C. The window always has the largest sum when values are negative.
  4. D. Sliding windows cannot handle more than one negative value.

Correct answer: A. Negative values break the monotonicity of window sums, so shrinking the window cannot guarantee the residue moves toward zero; prefix residues with a hash table are used instead.

With negatives, the window sum is not monotone in its endpoints, so a one-pass window cannot decide which side to move. Prefix residues with first-occurrence positions solve the same question.

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

Exams that ask this topic