Hello Programmers In this post, you will know how to solve the Andrew and the Meatballs Codechef Solution. The Problem Code: AMMEAT
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.
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 BrokenProgrammers. This tutorial is only for Educational and Learning purposes.
Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.