Took me a beat to realize this was just binary search on the answer.
This is a classic 'maximize the minimum' problem that can be solved using binary search on the answer combined with a greedy feasibility check. First, define the search space for the minimum gap (from 1 to total seats/(k-1)), then for a given gap, check if it's possible to place k people with at least that gap by greedily placing each person as early as possible. The maximum feasible gap is the answer.
Pro tip: Clarify with the interviewer whether the gap is measured as the number of seats between people or the distance between their positions (difference in indices). Also, mention that the greedy check runs in O(n) time, making the overall solution O(n log(total_seats)) which is efficient.
Recognize that we need to maximize the minimum distance between any two adjacent people. The answer lies between 1 and total_seats/(k-1), where total_seats is the sum of all seats.
Given a candidate minimum gap d, determine if it's possible to place k people such that each adjacent pair is at least d apart. Use a greedy approach: place the first person at position 0, then for each subsequent person, place them at the earliest position that is at least d away from the previous person.
Perform binary search over the possible gap values. For each mid value, run the feasibility check. If feasible, search for a larger gap; otherwise, search for a smaller gap.
After binary search converges, return the largest gap for which the feasibility check returns true.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.