# Andrew and the Meatballs Codechef Solution|Problem Code:AMMEAT

## Problem

Andrew likes meatballs very much.

He hasÂ NÂ plates of meatballs, here theÂ ithÂ plate containsÂ PiÂ meatballs. You need to find the minimal number of plates Andrew needs to take to his trip to Las Vegas, if he wants to eat there at leastÂ MÂ meatballs. Note that each plate is already packed, i.e. he cannot change the amount of meatballs on any plate.

### Input

The first line of the input contains an integerÂ T, denoting the number of test cases. The description ofÂ TÂ test cases follows. The first line of each test case contains two space-separated integersÂ NÂ andÂ M. The second line containsÂ NÂ space-separated integersÂ P1,Â P2, â€¦,Â PN.

### Output

For each test case, output an integer, denoting the minimum number of plates. If itâ€™s impossible to take at leastÂ MÂ meatballs, printÂ -1.

### Constraints

• 1Â â‰¤Â TÂ â‰¤Â 7777
• 1Â â‰¤Â NÂ â‰¤Â 7
• 1Â â‰¤Â M, PiÂ â‰¤Â 1018

### Sample Input 1

``````1
4 7
1 2 3 4``````

### Sample Output 1

``````2
``````

## Andrew and the Meatballs â€“ CodeChef Solution in JAVA

```import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int tc = 0; tc < T; tc++) {
int N = sc.nextInt();
long M = sc.nextLong();
long[] P = new long[N];
for (int i = 0; i < P.length; i++) {
P[i] = sc.nextLong();
}
System.out.println(solve(P, M));
}
sc.close();
}
static int solve(long[] P, long M) {
Arrays.sort(P);
long sum = 0;
for (int i = P.length - 1; i >= 0; i--) {
sum += P[i];
if (sum >= M) {
return P.length - i;
}
}
return -1;
}
}```

Disclaimer:Â The above Problem (Andrew and the Meatballs) is generated byÂ CodeChef but the solution is provided byÂ Â Chase2learn.This tutorial is only forÂ EducationalÂ andÂ LearningÂ purpose.

Sharing Is Caring