Data Structures & AlgorithmsVery hard

When two consecutive windows of length 3 are compared, why does equal sums reduce to a single element comparison?

  1. A. Windows of length 3 always have equal sums.
  2. B. Consecutive windows never share an element.
  3. C. The sums are compared digit by digit.
  4. D. The windows share two elements, so their sums differ only by the entering and leaving elements; equal sums mean those two are equal.

Correct answer: D. The windows share two elements, so their sums differ only by the entering and leaving elements; equal sums mean those two are equal.

Window i+1 drops A[i] and adds A[i+3], so the sums are equal exactly when A[i] = A[i+3]. Sliding-window updates make this a one-line check.

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

Exams that ask this topic