16 October 2014

Quiz 38: Find the minimum price of a product

Problem:
Given price offered for a product by N different companies. Find the minimum price and name of company

Input Format: 
n = no of offers
followed by n lines in format "price company name" ie separated by space.

Output Format: 
Minimum price is __ by ___

Constraints: 
none

Sample Input
5
17000 snapdeal
21000 amazon
16322 ebay
16321 flipkart
32000 homeshop18

Sample Output:
Minimum cost is 16321 by flipkart

Explanations:
Claerly minimum cost is 16321


Solution:

chomp($n=<STDIN>);
@data=();
for($i=0;$i<$n;$i++)
{
chomp($inp=<STDIN>);
@arr=split(" ",$inp);
push(@data,$arr[0]);
push(@data,$arr[1]);
}
%hash=@data;
@names = keys %hash;
@names = sort{$a<=>$b}@names;
$min = $names[0];
print "Minimum cost is $min by ";
print "$hash{$min}";



Tips:
Using HASH is best approach for such problems

No comments:

Post a Comment