Data Structures & AlgorithmsVery hard

Positive integers, two pointers count subarrays with product less than K. Why is right − left + 1 added for each right end?

  1. A. Every start index from left to right gives a valid subarray ending at right, since starting later only shrinks the product.
  2. B. Each right end contributes exactly one valid subarray regardless of left.
  3. C. Left never moves, so the width is the total count.
  4. D. Products of positive integers are always sorted by length.

Correct answer: A. Every start index from left to right gives a valid subarray ending at right, since starting later only shrinks the product.

Once [left..right] has product less than K, every shorter subarray ending at right (starting at any index in between) has a product no larger, so all of them are valid. The other options describe a different counting rule.

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

Exams that ask this topic