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?
- A. Every start index from left to right gives a valid subarray ending at right, since starting later only shrinks the product.
- B. Each right end contributes exactly one valid subarray regardless of left.
- C. Left never moves, so the width is the total count.
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Which statement about inversion counting with repeated values is correct?
- Houses stand in a circle with values [5, 1, 9, 8, 4, 6, 2, 7]. Adjacent houses cannot both be robbed, and the first and last houses are…
- A staircase has 8 steps with costs [25, 14, 7, 32, 2, 25, 28, 39]. You may start on step 0 or step 1, and each move climbs 1 or 2 steps…
- In how many different ORDERED ways can 10 be written as a sum of parts taken from {1, 3, 4}? Sums that differ only in the order of parts…
- Each digit string maps to letters with 1 to A ... 26 to Z. The string "2262012" is split into groups of one or two digits, and each group…
- For the array [3, -7, 4, -2, 5, -9, 6, 1], you may delete at most ONE element (the rest keep their order). Then you take a non-empty…