This fails for the sample test input - the output has to match exactly, remember!
No Sample input output is matching
If you say so
But itâs showing wrong answer?
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.
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
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.