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))
  1. A. 15
  2. B. 10
  3. C. 55
  4. 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

Exams that ask this topic