TopcoderArchiveVisit Topcoder
SRM12 Jul 2005
SRM 251

Elections

Problem statement, definition, constraints, and public examples.

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

  1. {"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.
  2. {"1222111122","2222222111","11111222221222222222"}
    Returns: 1
    The percentages of people, prefering candidate 1 to candidate 2 are (in order): 50%, 30%, 30%
  3. {"111","112","121","122","211","212","221","222"}
    Returns: 7
  4. {"1122","1221","1212","2112","2121","2211"}
    Returns: 0
  5. {"11112222111121","1211221212121","112111222","11122222222111","112121222","1212122211112"}
    Returns: 3
Back to all problems