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))- A. 3 -1
- B. 1 -1
- C. 2 -1
- 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
- What is printed? python people = [('Ann', 30), ('Bob', 25), ('Cid', 30), ('Dan', 25)] s1 = sorted(people, key=lambda p: p[0]) s2 =…
- What is printed? python from itertools import accumulate h = [3, 1, 4, 1, 5, 9, 2, 6] vis = [x for x, m in zip(h, [0] + list(accumulate(h…
- For an array of 10^6 numbers, which approach finds the largest sum of k consecutive elements fastest and correctly?
- Which statement about the bisect module is FALSE?
- What is printed? python def mink(piles, h): lo, hi = 1, max(piles) while lo < hi: mid = (lo + hi) // 2 if sum(-(-p // mid) for p in piles)…
- What is printed? python import heapq def huff(freqs): h = list(freqs) heapq.heapify(h) cost = 0 while len(h) > 1: a = heapq.heappop(h) b =…