7 September 2014

Quiz 22: Find maximum number of kites Prasoon can buy

Problem:
There is a Kite festival organized at Jaipur. Prasoon have Rs N with him and want to buy kites.
He goes to a shop and shopkeeper shows different kites to him. All different kites have unique price and there are only 1 kite of each type.
Now Prasoon want to maximum kites with money with me. Can you find maximum number of kites he can buy.

Input Format: 
N
Prices of different kites available separated by space

Output Format: 
Count of maximum numbers of kites Prasoon can buy

Constraints: 
none

Sample Input
50
1 34 46 3 8 11 200 16 24 2 33 5 

Sample Output:
7

Explanations:-
1+2+3+5+8+11+16<50, so count is 7

Solution:

chomp($n=<STDIN>);
chomp($str2=<STDIN>);
@arr2=split(" ",$str2);
@arr2=sort{ $a <=> $b }(@arr2);
$count=0;
$cost=0;
$i=0;
do
{
$cost=$cost+$arr2[$i];
$count++;
$i++;
}while($cost<=$n);
$count--;
print "$count";

Tips:
Sort the prices first and them add them till all money is used.

No comments:

Post a Comment