TopCoder problem "Elections" used in SRM 251 (Division II Level One)



Problem Statement

    There are two candidates campaigning to be president of a country. From newspaper polls, it is clear what percentages of people plan to vote for each candidate in each state. Candidate 1 wants to campaign in one last state, and needs to figure out which state that should be.



You are given a String[] likelihoods, each element of which corresponds to a state. Each element consists of the characters '1' and '2', where '1' represents some number of votes for candidate 1, and '2' represents votes for candidate 2 (in each element every character represents the same number of votes). You are to return an int representing the 0-based index of the state where the lowest percentage of people are planning on voting for candidate 1 (lowest percentage of '1' characters in that element of the input). If there are multiple such states, return one with the lowest index in likelihoods.
 

Definition

    
Class:Elections
Method:visit
Parameters:String[]
Returns:int
Method signature:int visit(String[] likelihoods)
(be sure your method is public)
    
 

Constraints

-likelihoods will contain between 1 and 50 elements inclusive.
-Each element of likelihoods will contain between 1 and 50 characters inclusive, and each character will be '1' or '2'.
 

Examples

0)
    
{"1222","1122","1222"}
Returns: 0
In the first state only 25% of people prefer candidate 1, while in the second and third, 50% and 25% prefer him, respectively.
1)
    
{"1222111122","2222222111","11111222221222222222"}
Returns: 1
The percentages of people, prefering candidate 1 to candidate 2 are (in order): 50%, 30%, 30%
2)
    
{"111","112","121","122","211","212","221","222"}
Returns: 7
3)
    
{"1122","1221","1212","2112","2121","2211"}
Returns: 0
4)
    
{"11112222111121","1211221212121","112111222","11122222222111","112121222","1212122211112"}
Returns: 3

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=4496

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=7226&pm=4496

Writer:

Vedensky

Testers:

PabloGilberto , lbackstrom , brett1479

Problem categories:

Brute Force