-
Notifications
You must be signed in to change notification settings - Fork 858
/
MarkAndToys.java
38 lines (36 loc) · 769 Bytes
/
MarkAndToys.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
*
* Problem Statement-
* [Mark and Toys](https://www.hackerrank.com/challenges/mark-and-toys/problem)
*
*/
package com.javaaid.hackerrank.solutions.algorithms.greedy;
import java.util.Arrays;
import java.util.Scanner;
/**
* @author Kanahaiya Gupta
*
*/
public class MarkAndToys {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int K = in.nextInt();
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
Arrays.sort(a);
int toyCount = 0, sum = 0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
if (sum <= K) {
toyCount++;
} else {
break;
}
}
System.out.println(toyCount);
in.close();
}
}