Problem Statement
Mark and Jane are very happy after having their first kid. Their son is very fond of toys, so Mark wants to buy some. There are N different toys lying in front of him, tagged with their prices, but he has only $K . He wants to maximize the number of toys he buys with this money.
Now, you are Mark's best friend and have to help him buy as many toys as possible.
Input Format
The first line contains two integers,N and K , followed by a line containing N space separated integers indicating the products' prices.
The first line contains two integers,
Output Format
An integer that denotes maximum number of toys Mark can buy for his son.
An integer that denotes maximum number of toys Mark can buy for his son.
Constraints
1<=N<=105
1<=K<=109
1<=price of any toy<=109
A toy can't be bought multiple times.
A toy can't be bought multiple times.
Sample Input
7 50
1 12 5 111 200 1000 10
Sample Output
4
Explanation
He can buy only 4 toys at most. These toys have the following prices: 1,12,5,10.
Copyright © 2015 HackerRank.
All Rights Reserved
All Rights Reserved
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args)throws IOException {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
String[] in = s.split(" ");
int n = Integer.parseInt(in[0]);
long k = Integer.parseInt(in[1]);
s = br.readLine();
long[] price = new long[n];
String[] temp = s.split(" ");
for(int i = 0 ; i < n ; i++)
{
price[i] = Integer.parseInt(temp[i]);
}
Arrays.sort(price);
int count = 0, sum = 0;
while(sum <= k)
{
sum += price[count];
count ++;
}
System.out.println(count - 1);
}
}
No comments:
Post a Comment