Andrew and the Meatballs Codechef Solution|Problem Code:AMMEAT

Hello coders, today we are going to solve Andrew and the Meatballs Codechef Solution|Problem Code:AMMEAT.

Andrew and the Meatballs Codechef Solution
Andrew and the Meatballs Codechef Solution


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.


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 P1P2, …, PN.


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.


  • 1 ≤ T ≤ 7777
  • 1 ≤ N ≤ 7
  • 1 ≤ M, Pi ≤ 1018

Sample Input 1 

4 7
1 2 3 4

Sample Output 1 


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(;
		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));
	static int solve(long[] P, long M) {
		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

Leave a Comment