Ballot Reservation Guarantees

Minimum extra votes guaranteeing each candidate a qualifying rank

HardBinary SearchSortingPrefix Sums

An election has n candidates. Candidate i already has votes[i] counted votes, and exactly remaining ballots have not yet been assigned. Each unassigned ballot can become one vote for any candidate.

A candidate qualifies if fewer than seats other candidates have strictly more votes. Ties therefore favor qualification: everyone tied at the qualifying boundary qualifies, even if this means more than seats candidates qualify.

For each candidate i, consider reserving exactly x of the unassigned ballots for that candidate. All remaining - x other ballots must then be distributed among the other candidates, in any way.

Find the smallest integer x that guarantees candidate i qualifies regardless of that distribution. Return -1 if no such reservation is possible.

Return an array in candidate-index order. Each candidate's answer is evaluated independently; the reservations in the returned array do not need to be achievable simultaneously.

Examples

Example 1

Input: votes = [4,5,2], remaining = 3, seats = 1
Output: [2,1,3]

For each candidate, reserve some ballots for that candidate and allow an adversary to distribute every other unassigned ballot. Because there is one qualifying rank, even one rival finishing strictly ahead prevents qualification. A tie does not.

Example 2

Input: votes = [6,6,3,1], remaining = 4, seats = 2
Output: [0,0,3,-1]

There are two qualifying ranks. To prevent a particular candidate from qualifying, the other ballots must make two different rivals finish strictly ahead of that candidate.

Example 3

Input: votes = [0,9,2], remaining = 5, seats = 3
Output: [0,0,0]

The number of qualifying ranks equals the number of candidates, so no distribution can leave any candidate with enough strictly stronger rivals to disqualify them.

Constraints

  • 1 <= votes.length <= 100000
  • 0 <= votes[i] <= 1000000000
  • 0 <= remaining <= 1000000000
  • 1 <= seats <= votes.length
  • Every unassigned ballot contributes exactly one vote to exactly one candidate.

The intended solution takes O(n log n + n log(remaining + 2) log n) time and O(n) space. Vote totals, prefix sums, and cost products can exceed 32-bit integer range, but all required integer arithmetic is exact with JavaScript Number under these constraints.

Hints

Show hint 1

For a fixed reservation, determine the fewest ballots an adversary needs to make seats rivals finish strictly ahead. Which rivals are cheapest to raise?

Show hint 2

Sort the current vote totals and build prefix sums. The relevant rivals are the strongest seats candidates other than the candidate being tested. Their total raising cost can be evaluated using a binary search in the sorted totals.

Follow-up questions

What an interviewer might ask once you have a working solution.

  • How would the cost calculation change if ties at the boundary caused disqualification instead?
  • Can you return, for each impossible reservation guarantee, one distribution of the remaining ballots that defeats that candidate even after reserving all available ballots?

Practice this with an AI interviewer

Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.

Start this problem