The break
means you stop accepting input that should belong to that test case, which possibly feeds the next test case with garbage input.
great… do it then…
#include
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int e,c,sum=0;
cin>>e>>c;
int arr[e];
for(int i=0;i<e;i++)
{
cin>>arr[i];
}
for(int i=0;i<e;i++)
{
sum = sum + arr[i];
}
if(sum < c)
cout<<“YES”<<endl;
else
cout<<“NO”;
}
}
#include
using namespace std;
int main() {
// your code goes here
int test;
cin>>test;
while(test–)
{
int n,k;
cin>>n>>k;
int a[n],i;
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
k=k-a[i];
if(k>=0)
cout<<“Yes\n”;
else
cout<<“No\n”;
}
return 0;
}
For LECANDY problem, br.readLine() returns null with run option (this nullPointerException). It works fine when executed with custom input. What is the problem? Submission was accepted as well. I guess I have to run with custom input always
Where am I going wrong?
#include
using namespace std;
#define ull unsigned long long
int main() {
int t;
cin>>t;
while(t--)
{
int n;
ull c;
cin>>n>>c;
int flag = 0;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
if(a[i]>c)
{
flag = 1;
break;
}
else
{
c-=a[i];
}
}
if(flag == 0)
cout<<"Yes\n";
else
cout<<"No\n";
}
return 0;
}
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!
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!
Edit:
He solved it. Maybe trying to “Run” without Providing “Custom Input”?
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.
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