You are not logged in. Please login at www.codechef.com to post your questions!

×

LECANDY - Editorial

15
1

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.

This question is marked "community wiki".

asked 11 Jun '12, 22:15

admin's gravatar image

0★admin ♦♦
19.8k350498541
accept rate: 36%

edited 25 Dec '12, 15:04

really nice, waiting for the next editorials ;-)

(11 Jun '12, 23:31) betlista ♦♦3★
1

This can't be better!! :D

(12 Jun '12, 00:25) javadecoder4★

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

link

answered 14 Oct '17, 21:19

dipjul's gravatar image

0★dipjul
1
accept rate: 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; }

link

answered 14 Dec '17, 18:38

kk_the_coder's gravatar image

1★kk_the_coder
1
accept rate: 0%

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? :( /

link

answered 19 Dec '17, 00:41

pari97's gravatar image

0★pari97
32
accept rate: 0%

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;

}

link

answered 24 Dec '17, 22:00

dvarc33343's gravatar image

0★dvarc33343
1
accept rate: 0%

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();
    }
}
link

answered 09 Jan '18, 09:07

sank20's gravatar image

2★sank20
1
accept rate: 0%

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"); }

link

answered 22 Jan '18, 11:58

az1az09_3143's gravatar image

0★az1az09_3143
1
accept rate: 0%

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(' '))
link
This answer is marked "community wiki".

answered 28 Jan '18, 11:32

kd90's gravatar image

2★kd90
1
accept rate: 0%

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?

link

answered 29 Jan '18, 17:58

ketkar's gravatar image

3★ketkar
1
accept rate: 0%

i guess, you must put this part:

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

outside loop and without break.

link

answered 02 Jun '18, 14:47

kadeksuryam's gravatar image

0★kadeksuryam
1
accept rate: 0%

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

link

answered 09 Jul '18, 14:59

datasmith's gravatar image

1★datasmith
1
accept rate: 0%

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

link

answered 31 Jul '18, 14:28

mystique17's gravatar image

2★mystique17
1
accept rate: 0%

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])

    }


}
link

answered 01 Aug '18, 23:38

pbis123's gravatar image

0★pbis123
1
accept rate: 0%

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

link

answered 04 Sep '18, 08:47

durgeshk's gravatar image

0★durgeshk
1
accept rate: 0%

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

link

answered 04 Sep '18, 08:50

durgeshk's gravatar image

0★durgeshk
1
accept rate: 0%

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

(04 Sep '18, 09:17) joffan5★

5TREYHHTRJ NHYHJKM JGM

link

answered 05 Nov '18, 13:18

himhemant's gravatar image

0★himhemant
1
accept rate: 0%

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


}
link

answered 11 Dec '18, 23:25

pvtejeswar's gravatar image

0★pvtejeswar
11
accept rate: 0%

edited 11 Dec '18, 23:27

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×15,851
×1,688
×861
×18
×7

question asked: 11 Jun '12, 22:15

question was seen: 12,338 times

last updated: 11 Dec '18, 23:27