MAXIMUM SUBARRAY SUM with kadane algo


It is Giving Run Error

#include
using namespace std;
int main()
{ int t;
cin>>t;
while(t>0)
{

 int n;
 cin>>n;
 int a[1000];
 int cs=0;
 int ms=0;
 for(int i=0;i<n;i++)
 {
	 cin>>a[i];
 }
  
  for(int i=0;i<n;i++)
  {
	  cs=cs+a[i];
	  if(cs<0)
	  {
		  cs=0;
	  }
	  ms=max(cs,ms);
  }

 cout<<ms<<endl;
 t--;

}
return 0;
}

@kkant107 Hey Kamal, you are forgetting to add testcase value in input. :slight_smile:

cin>>t i have written that

In Input value
4 -2 5 -2 2

you are forgetting to add 1 for testcase value.

I am not getting you

tell me in this input 4 -2 5 -2 2 what is value of t variable.

The Person has not entered any value for test case nor the n

Like here 2 is given and for that Have written
int t
cin>>t;
2
4
1 2 3 4
3
-10 5 10

1 = testcase Value //you forgot this.
4 = Array Size
-2 = a[0]
5 = a[1]
-2 = a[2]
2 = a[3]

In Sample Input
2 β€”> tescase Value
4 ----> Array Size
1 2 3 4
a[0] a[1] a[2] a[3]

3 -------> arraySize
-10 5 10
a[0] a[1] a[2]

How should i change code For value
1 = testcase Value //you forgot this.

Your code is perfect see in the input area add 1 for testcase before 4

No Code is Running and giving right output but when I use
Submit button it is Giving
Run error

Array Size range in the problem
1 <= N <= 100000

you did a[1000]

correct it as a[100000].

thank u so much it worked :slight_smile:

1 Like

I hope I’ve cleared your doubt. Please mark this doubt as resolved and rate me on the basis of your experience. Rating option is just below where you mark this doubt as resolve. Your feedback is very important. It helps us to 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.

Resolve section

From where i can rate and mark it resolved

@kkant107 Hey Kamal, It’s not on this doubt Page. You can find it at the ask doubt section of your course curriculum.