There was a problem called little elephant and candies and here are the link and code I wrote respectively :

My code:

``````#include <iostream>
using namespace std;

int main()
{
int number;
cin>>number;
int elephants,candies,total_happy;

for(int i=0;(i<number)&&(number<=1000);i++)
{
total_happy=0;

int happy[elephants];

cin>>elephants>>candies;/// First line of input

for(int i=0;i<elephants;i++) /// Second line second line of input
{
cin>>happy[i];
total_happy+=happy[i];
}

if(total_happy>candies){
cout<<"No"<<endl;
}
else{
cout<<"Yes"<<endl;

}

}

}

``````

I honestly feel my logic is right because it works for all the test cases. I don’t understand why it says wrong when I submit it.
It sometimes even says Runtime error when i run the code but when I refresh and run it again, it gives correct output

You declare an array of size `elephants` before you know what the value of `elephants` should be. Surprised there is no compiler warning for this.

1 Like

I changed that part of the code but it still says wrong answer

``````  cin>>elephants>>candies;/// First line of input

int happy[elephants];
``````

Consider the test input:

``````1
100 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
``````
1 Like

In that case total_happy will be equal to 100, candies will be equal to 1
since total_happy>candies it SHOULD output “No” -

I know Your most recent submission outputs `Yes`.

It was supposed to be elephants<=100
Thanks a ton man

1 Like