Doubt why its printing for last testcase only?

here is my code

#include

using namespace std;

int main()
{

int T,N,i,j,k;
cin>>T;
int arr[15],sum,maxsum=0;
for( ;T>0;T--)
{
	cin>>N;
	for(i=0;i<N;i++)
	{
		cin>>arr[i];
	}
}
for(i=0;i<N;i++)
{
	for(j=i;j<N;j++)
	{
		sum=0;
		for(k=i;k<=j;k++)
		{
			sum+=arr[k];
		}
		if(sum>maxsum)
		{
			maxsum=sum;
		}
	}
}
cout<<maxsum;

return 0;

}

when i am inputting it for 2 test cases its giving the ans for last test case only. i am not able to rectify it ?

Remove the closing curly bracket of the loop for(;T>0;T–) and put it just before the return 0 statement.

Hope it Helps.

ya its resolved now . but now when i am compiling it its giving run error for all test cases . a exclamation mark is coming on all test cases.

Hi @ritik_99

It is because of the declaration of the array outside the test cases loop. Also maxsum and sum should be initialised to 0 for every test case. Then also your code would time out. I suggest you to first watch the video of maximum subarray sum / Kadane’s Algorithm from your course and then attempt this question.

If your doubt is resolved, please mark it as resolved.

so its because of complexity that its timing out ?

Yes…