ProgrammingHardCognizant GenC · TCS NQT
What is printed?
What is printed?
def min_len(a, t):
best = float('inf')
s = 0
l = 0
for r, x in enumerate(a):
s += x
while s >= t:
best = min(best, r - l + 1)
s -= a[l]
l += 1
return best if best != float('inf') else 0
print(min_len([2, 3, 1, 2, 4, 3], 7), min_len([1, 1], 5))- A. 3 0
- B. 2 0
- C. 2 -1
- D. 4 0
Correct answer: B. 2 0
The shortest window with sum at least 7 is [4, 3], of length 2. When no window reaches the target the function returns 0, not -1.
More Python Programming questions
- What is printed? python import heapq def kthlargest(a, k): h = [] for x in a: heapq.heappush(h, x) if len(h) > k: heapq.heappop(h) return…
- What is printed? python from collections import deque def winmax(a, k): dq = deque() out = [] for i, x in enumerate(a): while dq and…
- What is printed? python from collections import deque g = {1: [2, 3], 2: [4], 3: [4, 5], 4: [6], 5: [], 6: []} dist = {1: 0} q =…
- What is printed? python from functools import cache @cache def total(xs): return sum(xs) try: print(total((1, 2, 3))) print(total([1, 2…
- You must choose the maximum number of non-overlapping activities, each with a start and a finish time. Which greedy rule is correct?
- What is printed? python a = [4, 1, 7, 3, 2] pre = [0] for x in a: pre.append(pre[-1] + x) def rng(l, r): return pre[r] - pre[l - 1]…