APT: Vote Rigging

Class

public class VoteRigging { public int minimumVotes(int[] votes) { // fill in code here } }

Problem Statement

You have used your secret mind-reading device to find out how every voter will vote in the next election. Your mind-reading device also revealed to you that all the voters are prepared to change their vote if you pay them enough.

The ith element of votes is the number of people who were originally planning to vote for candidate i. Return the minimum number of votes that you must change to ensure that candidate 0 (your favorite) will have more votes than any other candidate.

Constraints

Examples

  1. 
    {5, 7, 7}
    
    Returns: 2
    
    
    Buying one vote from each of the other two candidates leaves candidate 0 with 7 votes and the others with 6 each.

  2. 
    {10, 10, 10, 10}
    
    Returns: 1
    
    
    You need strictly more votes than all other candidates, so you need to buy one vote.

  3. 
    {1}
    
    Returns: 0
    
    
    If you are the only candidate, you automatically win.

  4. 
    {5, 10, 7, 3, 8}
    
    Returns: 4