Data Structures & AlgorithmsVery hard

How many contiguous windows of length exactly 4 in 1 3 4 5 2 6 7 1 4 have a sum that is a perfect square?

  1. A. 0
  2. B. 1
  3. C. 2
  4. D. 3

Correct answer: B. 1

Test each length-4 window's sum with an exact square check; the sliding window gives each sum in O(1). Shifting the sum, using length three, or testing divisibility by 4 each answer a different question.

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

Exams that ask this topic