Minimum Largest Piece After K Cuts on a Log
Heaps, Sorting & SearchAuto-graded · 17 test casesLast updated
Tests binary search on the answer to find the minimum possible largest piece when splitting a log into at least K+1 pieces.
Minimum Largest Piece After K Cuts on a Log
You are given a log of length n (in centimetres). You must make exactly k cuts, each cut positioned at an integer centimetre mark strictly inside the log (i.e. at positions 1, 2, …, n-1). Each cut is independent — you may cut wherever you like.
After all k cuts the log is divided into k+1 pieces. Your goal is to minimise the length of the largest piece.
Return the minimum possible length of the largest piece.
Constraints
1 ≤ n ≤ 10^91 ≤ k ≤ min(n - 1, 2000)- All cuts must land at distinct integer positions strictly inside
[1, n-1]. - It is always possible to make
kcuts (guaranteed by the constraints above).
Examples
Example 1
Input: min_largest_piece(10, 2)
Output: 4
Example 2
Input: min_largest_piece(7, 3)
Output: 2
Example 3
Input: min_largest_piece(1000000000, 1)
Output: 500000000
Hints
Hint 1
Think about binary searching on the answer: for a fixed maximum piece length L, can you always place k cuts to ensure no piece exceeds L?
Hint 2
For a given candidate maximum length L, the minimum number of cuts needed is ceil(n / L) - 1. Check whether this is ≤ k.
Hint 3
The answer lies in the range [ceil(n / (k+1)), n]. Binary search for the smallest L where ceil(n / L) - 1 ≤ k.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach: Binary Search on the Answer
Key Insight
Instead of trying to enumerate all ways to place cuts, we binary search on the answer itself — the maximum piece length L.
For a fixed candidate L, we ask: "Can we place exactly k cuts so that no piece exceeds L?"
Because we are free to choose where the cuts go, the greedy optimum is to space cuts as far apart as possible while never exceeding L. This means each piece can be at most L cm long, so the minimum number of cuts required to cover the whole log is:
cuts_needed = ceil(n / L) - 1
If cuts_needed ≤ k the candidate L is feasible (we can always add dummy cuts that reduce piece sizes further, keeping the maximum ≤ L). Otherwise L is too small.
Binary Search Bounds
- Lower bound:
ceil(n / (k+1))— even withk+1equal pieces we cannot do better. - Upper bound:
n— in the worst case (k=0, but that's excluded) the whole log is one piece.
We binary search for the smallest feasible L.
Correctness
The feasibility predicate is monotone: if L is feasible then any L' > L is also feasible. This makes the search space a clean 0/1 boundary, and binary search converges to the exact minimum.
Complexity
- Time: O(log n) — the search space is at most
n ≤ 10^9, so at most ~30 iterations, each O(1). - Space: O(1).
Reference Solution
import math
def min_largest_piece(n: int, k: int) -> int:
lo, hi = math.ceil(n / (k + 1)), n
while lo < hi:
mid = (lo + hi) // 2
cuts_needed = math.ceil(n / mid) - 1
if cuts_needed <= k:
hi = mid
else:
lo = mid + 1
return lo