LECANDY - Editorial

PROBLEM LINKS:

Practice
Contest

DIFFICULTY:

CAKEWALK

PREREQUISITES:

Basic knowledge of arrays and loops.

PROBLEM:

The program asks you to calculate if all the elephants would get the number of candies that they want.

EXPLANATION:

If there are C candies in all, and elephant i needs A[i] candies, then it is possible to serve all the elephants only if there are enough candies available, i.e. the following condition must be satisfied:

C >= A[0] + A[1] + … + A[N-1]

This means you can have a simple loop over the array A to count the sum of the required number of candies by the elephants and then finally comparing it with C to determine the answer. If the above condition is satisfied, answer will be Yes, else the answer will be No (they have to be case-sensitive to avoid WA).

SETTER’S SOLUTION:

Can be found here.

APPROACH:

The problem setter used the above solution to solve the problem.

TESTER’S SOLUTION:

Can be found here.

APPROACH:

The problem tester used the above solution to solve the problem.

16 Likes

#include <stdio.h>

int main(){

int t,n,i,j;
int A[1000];
long long int c;
scanf("%d",&t);
for(i=0;i<t;i++){
scanf("%d",&n);
scanf("%lli",&c);
for(j=0;j<n;j++){
scanf("%d",&A[j]);
c=c-A[j];
}
if(c<0){
printf(“No\n”);
}
else{
printf(“Yes\n”);
}

}
return 0;
}

int main()
{
int t=0;
cin >> t;
while(t–){
unsigned int n=0,c=0,i,a[102], total=0;
cin >> n >>c;
for(i=0;i<n;i++){
cin>>a[i];
total=total+a[i];
}
if(total<=c)
cout<<“Yes\n”;
else
cout<<“No\n”;
}
return 0;
}

1 Like

#include <stdio.h>

int main(){
int arr[3] = {1,2,3 } ; //elephants

int candies[3] = {2,2,1 } ; //candies

int i ;

int *x;
x = &arr[i];
int *y;
y = &candies[i];

for(i=0; i<=2; i++ ) {
if (candies[ i ] >= arr[ i ]) {
printf("yes "); }
else
printf("no ");

}
}

/*
i guess my approach is correct. how can i improve my code? :frowning:
*/

2 Likes

#include<stdio.h>
int main()
{

int i,T,ii;
scanf("%d",&T);
for(i=0;i<T;i++)        //for test cases
{   int C,N;
    scanf("%d %d",&N,&C);
        int E[N];
     for(ii=0;ii<N;ii++)            //assigning candies to elephants
       {


        scanf("%d",&E[ii]);
            if(C>=ii)               //for assign only grater or equal index number candy to elephant
            {
                C=C-E[ii];
            }
            else {
                C=-1;
                }


       }
        if(C >=0)
        printf("Yes");
    else
        printf("No");
}
return 0;

}

1 Like

I’m getting NZEC and unable to understand why? Please help
import java.io.IOException;
import java.util.Scanner;

public class Main{

	public static void main(String args[]){
		lecandy();
	}
	
	public static void lecandy(){
		//Scanner in = new Scanner(System.in);
		try{
		java.io.BufferedReader r = new java.io.BufferedReader(new java.io.InputStreamReader (System.in));
		int t = Integer.parseInt(r.readLine());
		while(t>0){
			int N = Integer.parseInt(r.readLine());
			int C = Integer.parseInt(r.readLine());
//			int Ak[] = new int[N];
			int sum = 0;
			for(int i=0;i<N;i++){
//				Ak[i] = in.nextInt();
				sum+=Integer.parseInt(r.readLine());
			}
			if(sum<=C){
				System.out.println("Yes");
			}else{
				System.out.println( "No");
			}
			t--;
		}
		r.close();
		}catch(IOException e){
			e.printStackTrace();
		}
			
		
	//	in.close();
	}
}

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