ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
def cnt(a, k):
    p, l, res = 1, 0, 0
    for r, x in enumerate(a):
        p *= x
        while l <= r and p >= k:
            p //= a[l]
            l += 1
        res += r - l + 1
    return res
print(cnt([10, 5, 2, 6], 100), cnt([1, 2, 3], 0))
  1. A. 8 0
  2. B. 8 3
  3. C. 7 0
  4. D. 6 0

Correct answer: A. 8 0

This is a sliding window over subarrays whose product stays below k. For [10,5,2,6] with k=100 the windows add up to 8, and with k=0 no window qualifies, so the result is 0.

More Python Programming questions

Exams that ask this topic