LECANDY - Editorial

If you say so :slight_smile: :man_shrugging:

1 Like

But it’s showing wrong answer?:face_with_monocle:

please help, VS code had implemented but codechef IDE doen’t respond

#include <stdio.h>

int main() {
	int T;
	scanf("%d",&T);
	
	for(int i=0;i<T;i++){
	    int N,C;
	    scanf("%d %d",&N,&C);
		int candyRequired[N];
	    int sum=0;
	    
	    for(int j=0;j<N;j++){
	        scanf("%d",&candyRequired[j]);
	    }
	    
	    for(int j=0;j<N;j++){
	        sum = sum + candyRequired[j];
	    }
	    
	    if(sum <= C){
	        printf("Yes");
	    }
	    else{
	        printf("No");
	    }
	}
	return 0;
}

#include
using namespace std;

int main() {
int T,N,C,i,t;
int K[100];
std::cin >> T;
for(t=0;t<T;t++){
std::cin >> N >> C;
for(i=0;i<N;i++){
std::cin >> K[i];
}
for(i=0;i<N;i++){
C-=K[i];
}
if(C>=0){
std::cout << “Yes” << std::endl;
}else{
std::cout << “No” << std::endl;
}
}
return 0;
}

Can we just declare “int t” and not initialize it in the Line 3 of your code?
I am getting a “SIGTSTP error - Time limit exceeded” if I do not initialize t=0 while its declaration in my code. It works fine if I initialize t=0 while its declaration.
As I am taking its value as input in the very next line, why do I need to initialize it during declaration?
My solution code is here.

Are you trying to “Run” without Providing “Custom Input”?

It works fine if I provide the sample test cases in custom input. If I try to run it without using custom input, then this problem occurs.

#include
using namespace std;

int main() {
// your code goes here
int t;
cin>>t;
while(t–)
{
int n,c;
cin>>n>>c;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int s=0;
for(int i=0;i<n;i++)
{
s=s+a[i];
}
if(s<=c){
cout<<“Yes”<<"\n";
}
else{
cout<<“No”<<"\n";
}
}
return 0;
}

Could someone please explain, why this code is not working? TIA!

#include <iostream>
using namespace std;

int main()
{
int t, n, c, a;
bool happy = true;

cin >> t;

for (int i = 0; i < t; i++)
{
    cin >> n >> c;
    for (int j = 0; j < n; i++)
    {
        cin >> a;
        c -= a;
        if (c < 0)
        {
            happy = false;
            break;
        }
    }
    if (happy)
    {
        cout << "Yes" << endl;
    }
    else
    {
        cout << "No" << endl;
    }
}

return 0;
}

Your solution fails on the sample testcase (it only prints one value instead of the required two).

Hint: The cause for this particular issue is on this line:

for (int j = 0; j < n; i++)

Edit:

Once you’ve fixed that issue, consider the test input:

3
2 3
1 1
3 7
4 2 2
2 3
1 1
2 Likes

Thank you very much for helping! I fixed that line and also changed the position of initializing “Bool Happy”. It worked fine on the test case that you gave but it still fails when I submit.

#include <iostream>
using namespace std;

    int main()
    {
    int t, n, c, a;

    cin >> t;

    for (int i = 0; i < t; i++)
    {
        cin >> n >> c;
        bool happy = true;
        for (int j = 0; j < n; j++)
        {
            cin >> a;
            c -= a;
            if (c < 0)
            {
                happy = false;
                break;
            }
        }
        if (happy)
        {
            cout << "Yes" << endl;
        }
        else
        {
            cout << "No" << endl;
        }
    }

    return 0;
    }

Now consider the test input:

2
2 1
5 1
4 4
1 1 1 2

In the second iteration of this test case i.e. i = 1, the result is wrong. But I can’t see what’s the problem, because when I try the same test case on its own i.e.

1
4 4
1 1 1 2

,it works properly.

There’s a major clue, right there - keep debugging!

Finally, Got it!
My code was assigning the previously unused value of “a” to the next variable in code, which messed up everything.
Thank you for making me learn it myself by not telling the answer right away.

1 Like

Video Solution

video solution

I’m new to codechef. Here’s my solution. Can someone give some feedback regarding this solution?

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {
    int T, N;
    unsigned long int C, C2, temp;
    cin >> T;
    vector<string> arr(T);

    for(int i=0; i < T; i++){
        cin >> N >> C;
        C2 = 0;
        for (int j=0; j < N; j++){
            cin >> temp;
            C2 += temp;
        }
        if (C2 > C)
            arr[i] = "No";
        else
            arr[i] = "Yes";
    }

    for(auto& i: arr)
        cout << i << endl;

    return 0;
}

i think you need to take STL , it will work better