LECANDY - Editorial

import java.util.Scanner;
class LEAC {

public static void main(String[] args) {
	Scanner sc=new Scanner(System.in);
	Scanner r=new Scanner(System.in);
	int T,N,C;
	String s="";

	while(sc.hasNextLine())
	{
		
	
	s=sc.nextLine();
	T=Integer.parseInt(s);
	if(T>=1 && T<=1000)
	{
		for(int j=0;j<T;j++)
		{
			s=r.nextLine();
			//r.nextLine();
			String S[]=s.split(" ");
			N=Integer.parseInt(S[0]);
			if(N>=1 && N<=100)
			{
			C=Integer.parseInt(S[1]);
			if(C>=1 && C<=1000000000)
			{
				int A[]=new int[N];
				s=r.nextLine();
				S=s.split(" ");
				for(int k=0;k<N;k++)
				{
					A[k]=Integer.parseInt(S[k]);
				}
				for(int i=0;i<N;i++)
				{
					C=C-A[i];
				}
				if(C>=0)
				{
					System.out.println("Yes");
				}
				else
				{
					System.out.println("No");
				}
			}
			}
		}
	
	}
	}
}

}

Below is my code. I m getting wrong answer . Can someone point out where i m getting it wrong?

#include <iostream>
using namespace std;

int main() {
	int T;
	cin>>T;
	while(T--){
	    bool flag = false;
	    long long n,c,x;
	    cin>>n>>c;
	    for(int i=0;i<n;i++){
	        cin>>x;
	        if(x>c){  //if cant fullfil the need ,break and print No
	            flag = true;
	            break;
	        }
	        c -= x;
	    }
	    if(flag)
	        cout<<"No";
	    else
	        cout<<"Yes";
	    if(T)
	        cout<<"\n";
	}
	return 0;
}

Its so naive. I too missed that… Thanks you

int N = Integer.parseInt(r.readLine());
int C = Integer.parseInt(r.readLine());

The error is over here. You’re reading a line of String which contains multiple numbers and converting it into an integer.
For eg. if input it “10 15”, you’re doing “int N = 10 15”.
I hope that you get what I’m saying.

This is my solution, I hope it helps:
import java.util.;
import java.lang.
;
import java.io.;
/
Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for(int j=0;j<T;j++)
{
// elecan stores number of elephants and number of candies
// candyHappy stores number of candies required by elephants to be happy
// both in string format which is then converted into a integer array
String elecan = br.readLine();
int arr[] = new int[2];
String[] strs = elecan.trim().split(“\s+”);

        for (int i = 0; i < strs.length; i++) {
            arr[i] = Integer.parseInt(strs[i]);
        }
        //arr[0] is number of elephants and arr[1] is number of candies available
        String  candyHappy = br.readLine();    
        int can[] = new int[arr[0]];
        String[] str = candyHappy.trim().split("\\s+");
        
        int sum =0;
        for (int i = 0; i < str.length; i++) {
            can[i] = Integer.parseInt(str[i]);
            sum += can[i];
            if(sum>arr[1]){
                System.out.println("No");
                return;
            }
        }

        System.out.println("Yes");
	    
    }
}

}

I am getting NZEC error.

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		int n=0,c=0;
		boolean flag=true;
		for(int i=0;i<t;i++){
		    n=sc.nextInt();
		    c=sc.nextInt();
		    for(int j=0;j<n;j++){
		        c=c-sc.nextInt();
		        if(c<0){
		            flag=false;
		            break;
		        }
		    }
		    if(flag==false){
		        System.out.println("No");
		    }else{
		        System.out.println("Yes");
		    }
		    flag=true;
		}
	}
}

Can someone tell the error?

You don’t seem to have made any Submissions. Are you trying to “Run” without providing Custom Input?

Yeah that was precisely the problem!

1 Like

T=int(input())
for i in range(T):
N,C=map(int,input().split())
A=[]
A=list(map(int,input().split(’ ')))
sum=C
flag=0
for j in range(N):
sum=sum-A[j]
if(sum<0):
flag=1
break;
if(flag==0):
print(‘Yes’)
else:
print(‘No’)

#include <stdio.h>

int main(void) {
// your code goes here
int satisfactionCandies;
int noOfElephants[10],test,noOfCandies;
int i;
scanf("%d",&test);
for(i=1;i<=test;i++)
{
scanf("%d",&noOfElephants[i]);

    	scanf("%d",&noOfCandies);
}
for(i=1;i<=noOfElephants[i];i++)
{
    scanf("%d",&satisfactionCandies);
}


while(noOfCandies>=satisfactionCandies)
{
    noOfCandies--;
    printf("Yes");
}
printf("No");

return 0;

}

Can anyone help me out.What is the mistake in above code.

Why i am getting NZEC Error

class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int testcases = sc.nextInt();
while(testcases > 0){
int elephant = sc.nextInt();
int candies = sc.nextInt();
int[] candiesArr = new int[elephant];
for(int i=0; i<candiesArr.length; i++){
candiesArr[i] = sc.nextInt();
}
int totalCandies = 0;
for(int i=0; i<candiesArr.length; i++){
totalCandies += candiesArr[i];
}
String result = totalCandies > candies ? “No” : “Yes”;
System.out.println(result);
testcases–;
}
}
}

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

Edit:

He solved it. Maybe trying to “Run” without Providing “Custom Input”?

1 Like

what is the problem with this code:

while t:
    total=0
    n,c=map(int,input().split())
    k=input().split()
    for i in range(n):
        total+=int(k[i])
    if total<=c:
        print('YES')
    else:
        print('NO')
    t=t-1```

t is not defined anywhere.

2 Likes

Please check this

# cook your dish here
t=int(input())
while t:
    total=0
    n,c=map(int,input().split())
    k=input().split()
    for i in range(n):
        total+=int(k[i])
    if total<=c:
        print('YES')
    else:
        print('NO')
    t=t-1
2 Likes

This fails for the sample test input - the output has to match exactly, remember! :slight_smile:

1 Like

No Sample input output is matching

If you say so :slight_smile: :man_shrugging:

1 Like

But it’s showing wrong answer?:face_with_monocle:

please help, VS code had implemented but codechef IDE doen’t respond

#include <stdio.h>

int main() {
	int T;
	scanf("%d",&T);
	
	for(int i=0;i<T;i++){
	    int N,C;
	    scanf("%d %d",&N,&C);
		int candyRequired[N];
	    int sum=0;
	    
	    for(int j=0;j<N;j++){
	        scanf("%d",&candyRequired[j]);
	    }
	    
	    for(int j=0;j<N;j++){
	        sum = sum + candyRequired[j];
	    }
	    
	    if(sum <= C){
	        printf("Yes");
	    }
	    else{
	        printf("No");
	    }
	}
	return 0;
}

#include
using namespace std;

int main() {
int T,N,C,i,t;
int K[100];
std::cin >> T;
for(t=0;t<T;t++){
std::cin >> N >> C;
for(i=0;i<N;i++){
std::cin >> K[i];
}
for(i=0;i<N;i++){
C-=K[i];
}
if(C>=0){
std::cout << “Yes” << std::endl;
}else{
std::cout << “No” << std::endl;
}
}
return 0;
}