Question
You are given a one dimensional array that may contain both positive and negative integers, find the sum of contiguous subarray of numbers which has the largest sum. For example, if the given array is {-2, -5, 6, -2, -3, 1, 5, -6}, then the maximum subarray sum is 7.
Input Format:
The first line consists of number of test cases T. Each test case consists of N followed by N integers.
Constraints:
1 <= N <= 100000
1 <= t <= 20
0 <= A[i] <= 100000000
Output Format:
The maximum subarray sum
#include
using namespace std;
int main() {
int t;
cin >> t ;
while(t--){
int n;
cin >> n;
int arr[100];
int cs=0;
int ms=0;
for(int i=0; i<n ;i++){
cin >> arr[i];
}
for(int i=0; i<n ;i++){
cs+=arr[i];
if(cs<0){
cs=0;
}
ms=max(ms,cs);
}
cout << "max sum: "<<ms<<endl;
}
}
it is showing run-error in hacker blocks but ouputs is showing correct