14 September 2014

Quiz 29: Find number of people with weight above average weight

Problem:
Given weight of N number of people, find number of people whose weight is above average weight

Input Format: 
N followed by N weights

Output Format: 
Number of people whose weight is above average

Constraints: 
none

Sample Input
5 42 50 54 58 46

Sample Output:
2

Explanations:
Average is 50 and only 54 & 58 are above average, so answer is 2


Solution:

chomp($n=<STDIN>);
@arr=split(" ",$n);
$len=$arr[0];
$first=shift(@arr);
$sum=0;
foreach(@arr)
{
$sum+=$_;
}
$av=$sum/$n;
@arr=sort{$a<=>$b}(@arr);
$count=0;
for($j=$n-1;$j>=0;$j--)
{
if($arr[$j]>$av)
{
$count++;
}
else
{
last;
}
}
print "$count";


Tips:
Shift is used to take out first element of array

No comments:

Post a Comment