ProgrammingVery hardCodeforces-style · placement coding round

What is printed?

What is printed?
import bisect
def min_len(a, s):
    pre = [0]
    for x in a:
        pre.append(pre[-1] + x)
    best = float('inf')
    for i in range(len(pre)):
        j = bisect.bisect_left(pre, pre[i] + s)
        if j < len(pre):
            best = min(best, j - i)
    return best if best != float('inf') else -1
print(min_len([2, 3, 1, 2, 4, 3], 7), min_len([1, 2], 10))
  1. A. 3 -1
  2. B. 1 -1
  3. C. 2 -1
  4. D. 2 0

Correct answer: C. 2 -1

Prefix sums of positive numbers increase, so bisect finds the shortest window ending at each index. The window [4, 3] has length 2, and the whole array [1, 2] never reaches 10.

More Python Programming questions

Exams that ask this topic