3 December 2014

Quiz 63: Find the winning team

Problem:

In a cricket tournament, N matches were played. Sunil just have a data about winner of match i. The team with maximum number of wins is the winner of the tournament. Find the winner team. It is guaranteed that there is only 1 winner team.

Input Format: 

N
N lines having a winner if ith match


Output Format: 

name of winner team


Constraints: 

None

Sample Input

10
kkr
kkr
dd
mi
cs
dd
dd
cs
mi
rr

Sample Output:

dd

Explanations:

dd won 3 games which is highest



Solution:

chomp($n=<STDIN>);
%hash;
$max=1;
for(1..$n)
{
chomp($tmp=<STDIN>);
if($hash{$tmp})
{
$hash{$tmp}++;
if($hash{$tmp}>$max)
{
$max=$hash{$tmp};
$ans=$tmp;
}
}
else
{
$hash{$tmp}=1;
if($max==1){$ans=$tmp;}
}
}
print $ans;





Tips:

Maintain Hash and increment value to find maximum wins

No comments:

Post a Comment