my code is running smoothly, i’ve downloaded all the test cases also and their outputs too. Still on submitting the code it shows 2 test cases failed, run-error -_-
Test case not clearing even for correct answer
#include
#include
#include<limits.h>
using namespace std;
int main() {
int t,n,i,j,sum=0,m=INT_MIN,c=INT_MIN;
cin>>t;
int *total = new int[t];
total[t] = {0};
int **a = new int *[t];
for(i=0;i<t;i++)
{
cin>>n;
a[i] = new int[n];
for(j=0;j<n;j++)
{
cin>>a[i][j];
total[i] = total[i] + a[i][j];
if(-a[i][j]>=0 || sum - a[i][j] > 0)
{
sum = sum - a[i][j];
c = max(sum,c);
}
else
{
sum = 0;
}
}
c = total[i] + c;
sum = 0;
for(j=0;j<n;j++)
{
if(a[i][j]>=0 || sum + a[i][j] > 0)
{
sum = sum + a[i][j];
m = max(sum,m);
}
else
{
sum = 0;
}
}
cout<<max(c,m)<<endl;
sum=0;
c=INT_MIN;
m=INT_MIN;
}
return 0;
}
If you have downloaded all test cases…you can check for each test case manually to see where your code fails.
i cannot get why you are making a 2d array. That is not required and making your code more complicated.
Refer this:
my code isn’t failing for any test case on manually entering all the cases, still on submitting it gives me a error of test case failed.
And coming on to 2d array, ik it’s not required and it’s increasing the space time complexity, i’ll change that. But there’s no where written in the constraints about how complex the solution should be so why an error?
Run errors are mostly because of array index out of bounds…when you try to access an illegal space in memory. So most probably your code is giving run error because of that. Also i checked for a very large test case…for which your code was giving run error. Check for the test case entered in the sample input of above code.
Ohkay will recheck my code, thank you
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.