problem is:https://www.codechef.com/problems/LECANDY
my solution but it gives wrong answer :
#include
#include
using namespace std;
int main()
{ int t;
cin>>t;
int a,b,c1,c2;
while(t–)
{ cin>>a>>b;
vectorv;
int i=0,sum=0,x;
while(a–)
{cin>>x;
v.push_back(x);
sum+=v[i];
} if(sum<=b)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
}
I guess we really don’t need vectors for this problem.
I have attached the link to my solution [“click here”][1]
I have also listed the major changes you can make:
1)Remove Variables C1,C2.
2)Change variable sum from “int” to “long long”
4)And can remove vector.
Hope this is helpful to You,Happy coding:)
[1]: https://www.codechef.com/viewsolution/19410642
1 Like
hi i try this one it works:but my question is that whether c1 c2 make some error or vector is making error and sum works for int as well
#include
using namespace std;
int main() {
int t, a, b, sum, i;
cin>>t;
while(t–) {
cin>>a>>b;
sum=0;
while(a–) { cin>>i; sum=sum+i; }
if(sum<=b) cout<<“Yes”<<endl;
else cout<<“No”<<endl;
}
return 0;
}
@deepak_2100 Is the above code working.
Nope they are not
But we can reduce the memory used by our program
The issue with the main code was that we were adding or pushing the element at the back of Vector and trying to accessing it’s front.It will work if we change it to “push_front”.
You forgot to increment i. Add i++; after sum+=v[i];
int is sufficient for this problem. But using long long is good habit.
BeWare! with push_front on vectors. They take time proportional to size of vector. So If you are assuming time complexity to be O(n). But in fact they become O(n^2).
Instead use some other data structure for this purpose.
Other way round. Use push_back and run loop for i=n-1 to 0;
It can be actually doesn’t need any Data Structure except INT/LONG LONG
@aryanc403
1 Like