What is problem in this code

#include
#include
using namespace std;

int maxSubArraySum(int a[], long size)
{
int max_so_far = INT_MIN, m = 0;

for (int i = 0; i < size; i++) 
{ 
    m = m + a[i]; 
    if (max_so_far < m) 
        max_so_far = m; 

    if (m < 0) 
        m = 0; 
} 
return max_so_far; 

}

int main()
{
int k;
int A[100000000];
long n;
cin>>k;
cin>>n;
for(long l=0;l<k;l++)
{
for(long i=0;i<n;i++){
cin>>A[i];
}
int max_sum = maxSubArraySum(A, n);
cout << max_sum<<endl;
}
return 0;
}

@saranshjain18 Hey Saransh, Look at the constraints given.
1 <= N <= 100000

1 <= t <= 20

-100000000 <= A[i] <= 100000000

If we consider worst case than n = 100000
every element of i from 0 to 99999 would be 100000000
then maximum Sum wiil be = (N*Arr[0 to 99999]) = (100000 * 100000000) = (10^13)
so now int is not enough to store the maximum Sum that’s why you should take long long data type.

Hello @saranshjain18,

The following variables are not enough to stores the sum as
-100000000 <= A[i] <= 100000000 and 1 <= N <= 100000

In the worst case when all numbers are positive 100000000 and n is also 100000, then sum will be out of the range of int.
Solution: rather use long long int datatype.

Hope, this would help.
Give a like, if you are satisfied.

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.