Data Structures & AlgorithmsVery hardCodeforces Div2 D · binary search on answer
Problem: You binary search the answer x for 'the minimum possible largest sum when an array is split into k contiguous parts'. Which property makes this valid, and what is the complexity?
- A. Feasibility is not monotone, so ternary search is required with O(n log n)
- B. The answer is always the largest element, so no search is needed
- C. Feasibility of x is monotone (if x works, any larger x works), giving O(n log(sum))
- D. Use a segment tree, which gives O(n log n) with no monotonicity needed
Correct answer: C. Feasibility of x is monotone (if x works, any larger x works), giving O(n log(sum))
Greedily splitting with capacity x into at most k parts succeeds for every larger x whenever it succeeds for x, so the predicate is monotone. Each check is O(n) and the search space is about the total sum.
More Coding Interview Problems questions
- Problem: What is the time complexity of this loop in terms of n? python for i in range(1, n + 1): for j in range(i, n + 1, i): work()
- Problem: Count right/down paths from the top-left to the bottom-right of the 3x3 grid below, where '#' is blocked. The code returns the…
- Problem: What does the following print? python def add(x, bucket=[]): bucket.append(x) return bucket add(1) add(2) print(add(3))
- Problem: What does the following print for the array [7, 10, 4, 3, 20, 15] with k = 3? python import heapq def kth(a, k): h = [] for x in…
- Problem: What does the following print? python ops = [('b', 2), ('a', 2), ('c', 1), ('d', 3)] print([x for x, in sorted(ops, key=lambda t…
- Problem: Coins of values {1, 5, 6, 9} are available, unlimited copies each. What is the minimum number of coins needed to make exactly 11?