Painter's Partition Problem
A hard Binary Search problem included in Striver A2Z, Love Babbar 450. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Binary Search
- Sheets
- 2
- Core for
- 7 roles
- Platform
- GeeksforGeeks
The problem
There are n boards of different lengths and k painters. Each painter can only paint continuous sections of boards. Assign boards to painters so that the maximum time any painter spends is minimized. Return that minimum time.
Example 1
- Input
- boards = [10,20,30,40], k = 2
- Output
- 60
- Why
- Assign [10,20,30] to painter 1 and [40] to painter 2. Maximum time is 60.
Example 2
- Input
- boards = [5,10,30,20,40], k = 2
- Output
- 60
- Why
- Assign [5,10,20] to painter 1 and [30,40] to painter 2. Maximum time is 60.
Constraints
- 1 <= k <= boards.length <= 10^3
- 1 <= boards[i] <= 10^5
How to think about it
Updated 2026-09-09Minimizing the maximum painter load under contiguous partition constraints is identical to packing items into a fixed number of buckets without splitting an item. If a workload cap of T is feasible with k painters, any cap greater than T is also feasible, allowing a binary search on the workload cap.
Approaches, worst first
Dynamic programming partition
time O(k * n^2) · space O(k * n)
Compute prefix sums and define dp[i][j] as the minimum maximum workload allocating first i boards among j painters. Correct, but computes unnecessary subproblem states via nested transitions.
Binary search on max workloadWrite this one
time O(n log(sum(boards))) · space O(1)
Binary search the workload cap between max(boards) and sum(boards). For a tested time cap, greedily accumulate board lengths into the current painter until adding the next board forces handing off to another painter.
Where people lose marks · 3
- Initializing the lower bound below max(boards) produces an impossible test workload where a single board cannot be assigned to any painter.
- Failing to use 64-bit integers when accumulating prefix sums or searching limits where total board lengths exceed 2^31 - 1.
- Sorting boards array destroys contiguous assignment requirements; painters must receive original contiguous slices.
The theory behind it
Binary Search — the ground this problem stands on. All Binary Search problems
What Binary Search is
Binary search is the guessing strategy used when searching a thick telephone directory or guessing a secret number between one and a hundred. Rather than inspecting names one by one from the first page, the search opens straight to the middle page. If the target precedes that middle entry, the entire back half is discarded; if it follows, the front half is eliminated. Repeating this halved split finds the item with remarkable speed.
When to reach for it
Reach for binary search when queries target sorted collections, rotated sorted arrays, or monotonic answer spaces. Strong hints include logarithmic time constraints like O(log n) or search ranges exceeding one billion where stepping one value at a time times out. It also applies when validating whether a guessed solution is possible via a monotonic boolean check, known as binary search on answer.
How the pattern works
Define the search territory with two inclusive pointers, low and high. Compute the midpoint using low plus half the difference to high, avoiding integer overflow. Formulate an exact boolean condition that divides the range into true and false halves. Decide whether the boundary condition includes the midpoint or shifts strictly past it. The loop invariant states that the sought target, if it exists, remains trapped inside the interval throughout every iteration.
What each operation costs
| Operation | Time |
|---|---|
| find target in sorted array | O(log n) |
| find boundary in monotonic range | O(log n) |
What usually goes wrong with Binary Search
- Triggering integer overflow by computing middle using low plus high divided by two instead of low plus half of high minus low in fixed-width numeric types.
- Creating an infinite loop when the interval shrinks to two elements by setting low equal to mid when mid was rounded down.
- Mismatched loop condition and bounds updates, such as pairing low less than or equal to high with non-advancing pointer assignments that never terminate.
Which roles need this problem
Binary Search is a core topic for these 7 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 17 more roles, including Full-Stack Developer, Data Engineer, Android Developer.
Companies that have asked it
Tags taken from the problem's own GeeksforGeeks page — not a copied list.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Binary Search problems
Problem set and role mapping as of .