ProgrammingVery hardInfosys HackWithInfy · TCS CodeVita
What is printed?
What is printed?
def min_capacity(w, days):
lo, hi = max(w), sum(w)
while lo < hi:
mid = (lo + hi) // 2
need, cur = 1, 0
for x in w:
if cur + x > mid:
need += 1
cur = 0
cur += x
if need <= days:
hi = mid
else:
lo = mid + 1
return lo
print(min_capacity([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5))- A. 15
- B. 10
- C. 55
- D. 11
Correct answer: A. 15
Binary search on the answer between max(w) = 10 and sum(w) = 55, with a greedy packing check: 15 fits in 5 days as [1..5], [6,7], [8], [9], [10], while 14 needs 6. 10 and 55 are the search bounds, and 11 = ⌈55/5⌉ ignores that items cannot be split.
More Python Programming questions
- What is printed? python from functools import lrucache @lrucache(maxsize=2) def sq(x): return x x for v in [1, 2, 1, 3, 1]: sq(v)…
- What is printed? python def nge(a): res = [-1] len(a) st = [] for i, x in enumerate(a): while st and a[st[-1]] < x: res[st.pop()] = x…
- What is printed? python def pickbystart(iv): iv = sorted(iv) cnt, end = 0, -109 for s, e in iv: if s >= end: cnt += 1 end = e return cnt…
- In CPython, which operation on a collections.deque is O(n) in the worst case?
- What is printed? python def primes(n): s = [True] (n + 1) s[0:2] = [False, False] for i in range(2, int(n 0.5) + 1): if s[i]: s[i i::i] =…
- What is printed? python def merge(a, b): i = j = 0 out = [] while i < len(a) and j < len(b): if a[i] <= b[j]: out.append(a[i]) i += 1…