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 matchOutput Format:
name of winner teamConstraints:
NoneSample Input
10
kkr
kkr
dd
mi
cs
dd
dd
cs
mi
rr
kkr
kkr
dd
mi
cs
dd
dd
cs
mi
rr
Sample Output:
ddExplanations:
dd won 3 games which is highest
Solution:
%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