LECANDY - Editorial

int sumOfCandies = 0;
for(int i=0;i<A.leght;i++){
sumOfCandies = A[i] + sumOfCandies;
}
if(C >= sumOfCandies){
System.out.println(“Yes”);
}
else{
System.out.println(“No”);
}

This is for giving inputs in single line in python

N, C = map(float, input().split())

This is for giving inputs in a single line for list

A = []
A = map(float,input().split(' '))
2 Likes

int main(){
int t,n,c,toffeesNeeded,temp;
cin >> t;
while(t–){
cin >> n >> c;
toffeesNeeded = 0;
for (int i = 0; i < n; ++i)
{
cin >> temp;
toffeesNeeded += temp;
if (toffeesNeeded > c)
{
cout << “No” << endl;
break;
}
}
if (toffeesNeeded <= c)
{
cout << “Yes” << endl;
}
}
return 0;
}

GIves WA. Can someone find the mistake?

i guess, you must put this part:

if (toffeesNeeded > c)
        {
            cout << "No" << endl;
            break;
        }

outside loop and without break.

A common mistake of everyone who is getting “WA” in spite of correct logic is printing “YES” or “yes” instead of “Yes”.

2 Likes

import java.util.Scanner;

class LittleElephantsNCandy_LECANDY {

public static void main(String args[]){
	
	Scanner sc = new Scanner(System.in);
	
	int T = sc.nextInt();
	
	while(T > 0){
		
		int N = sc.nextInt();
		long C = sc.nextLong();
		String result = "Yes";
		
		while(N > 0){
			
			long k = sc.nextLong();
			
			C -= k;
			
			if(C < 0){
				result = "No";
				break;
			}
			
			N--;
		}
		
		System.out.println(result);
		T--;
	}
}

}

I am getting WA. Can anyone explain why

2 Likes

public class Firstarray {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	
	int i;
	
	Scanner sc = new Scanner(System.in);
	System.out.print("Enter the number of Candies = ");
	int c=sc.nextInt();
	System.out.print(c);
	System.out.print("Enter the number of Elephant = ");
	int n=sc.nextInt();	
	System.out.print(n);
	int[] arr1= new int[n];
	int l=arr1.length-1;
	for(i=0;i<=arr1.length-1;i++)
	{
		arr1[i]=sc.nextInt();
	}
	if(c>=l )
		System.out.println("true");
	else
		System.out.println("false");

// System.out.println("Enter the number of Elephant = "+n);
// for(i=0;i<=A[n-1];i++)
// {
// if(c>=A[n-1])

	}
	

}

Hi,

What is the problem with the following code?

#include <iostream>
using namespace std;

int main()
{
	int T;
	cin >> T;
	for (int i = 0; i < T; i++){
		int N;
		int C;
		cin >> N >> C;
		int sum = 0;
		
		for (int k = 0; k < N; k++) {
			int temp = 0;
			cin >> temp;
			sum += temp;			
			if (sum > C) 
				break;
		}
		if (sum > C) 
			cout << "No" << endl;
		else 
			cout << "Yes" << endl;
	}
    return 0;
}

If, I don’t use break statement within for loop, I get correct result. But, I couldn’t understand why using a break statement will alter the result.

Thanks in Advance.
durgesh

Hi,

What is the problem with the following code?

#include <iostream>
using namespace std;

int main()
{
	int T;
	cin >> T;
	for (int i = 0; i < T; i++){
		int N;
		int C;
		cin >> N >> C;
		int sum = 0;
		
		for (int k = 0; k < N; k++) {
			int temp = 0;
			cin >> temp;
			sum += temp;			
			if (sum > C) 
				break;
		}
		if (sum > C) 
			cout << "No" << endl;
		else 
			cout << "Yes" << endl;
	}
    return 0;
}

If, I don’t use break statement within for loop, I get correct result. But, I couldn’t understand why using a break statement will alter the result.

Thanks in Advance.
durgesh

5TREYHHTRJ NHYHJKM JGM

#include <stdio.h>
#include <stdlib.h>

struct AKList{
  long int N;
  long int C;
  int *myList;
  struct AKList *next;
};

int main(void){
  int NoOfTestCases;
  long int *NCArray = malloc(2 * sizeof(long int));
  struct AKList *AKArray = malloc(sizeof(struct AKList));
  struct AKList *AKArrayPtr = AKArray;
  long int number;
  long int sum = 0;

  scanf("%d", &NoOfTestCases );
  if(NoOfTestCases < 1 || NoOfTestCases > 1000) return -1;


  for (int i = 0; i < NoOfTestCases; i++){
    scanf("%ld", (NCArray) );
    scanf("%ld", (NCArray+1) );
    if((NCArray[0]<1 || NCArray[0] > 100 )||(NCArray[1] < 1 || NCArray[1] > 1000000000 )) return -1;
    AKArrayPtr->myList = malloc(NCArray[0] * sizeof(long int));
    AKArrayPtr->N = NCArray[0];
    AKArrayPtr->C = NCArray[1];

    for(int k = 0; k < NCArray[0]; k++){
      scanf("%ld", &number);
      if(number > 0 && number <= 10000){
        AKArrayPtr->myList[k]= number;
      }
    }
    AKArrayPtr->next = malloc(sizeof(struct AKList));
    AKArrayPtr = AKArrayPtr->next;
  }
  AKArrayPtr->next = NULL;
  AKArrayPtr = AKArray;
  for(int i = 0; i < NoOfTestCases; i++){
    sum = 0;
    for(int i = 0; i < AKArrayPtr->N; i++){
      sum += AKArrayPtr->myList[i];
    }
    if(sum > AKArrayPtr->C){
        printf("No\n");
    }
    else{
      printf("Yes\n");
    }
    AKArrayPtr = AKArrayPtr->next;
  }


}

Actually there is no need of using array also we can simply add the values in sum variable and compare it with the number of candies if it is less or equal then answer is yes else no.

really nice, waiting for the next editorials :wink:

1 Like

This can’t be better!! :smiley:

2 Likes

The break means you stop accepting input that should belong to that test case, which possibly feeds the next test case with garbage input.

2 Likes

great… do it then…

1 Like

#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;

}