Maximum subarray sum in the challenge section

#include
using namespace std;

int main()
{
int t;
cin>>t;
int a[100];
if(t<=20 && t>=1)
{

    while(t--)
    {   int n;
        cin>>n;
        if(n>=1 && n<=100000)
        {
            int i,maxsum=0,currentsum=0;
            for(i=0;i<n;i++)
            {   if(a[i]>=0 && a[i]<=100000000)
            	    cin>>a[i];
            }
            for(i=0;i<n;i++)
            {
	            currentsum=currentsum + a[i];
	            if(currentsum<0)
	            {
		            currentsum=0;
	            }
	            maxsum=max(currentsum,maxsum);
            }
            cout<<maxsum<<endl;
        }
    }
}

return 0;
}
///Could you please tell me why I am getting a run time error?

ashish value of n can be upto 100000 but u have taken array of 100 size so thats why run time error is coming increase the size of array 100000 u will not get tle

1 Like

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.

Sir I change the array size as you said. BUT now the output is wrong answer for the test case.
kindly please correct my code once again.

as u have taken array before test cases so 1 array will be common to all test cases but u need a new array for each test case so declare array after the while loop of test case

After doing this too , wrong answer.

ashish the value of a[i] can be negative also as u can see in test case
so the constarint u put in if condition while taking input remove that
a[i]>=0 && a[i]<100000 remove this one ans take simplly cin>>a[i]

BUt in the question the constraints for a[i] is mentioned!

see the test case
in the question it is
-1000000…<a[i]<100000…
the question have some glitch thats why only 0 is shown

I removed the constraint but it is showing run-time error.

see this


i think u have done the same mistake of array size

Yes sir,initially I had changed the array size but in later submissions I did not. That’s why it was showing run time error. Thanks a million.

ok.if ur doubts is resolved then mark it resolved and gave the feedback

1 Like