i am getting additional 10 in my final answer. first i did enter the number of test case, then entered the array, then formed the another array called sum which accounts the sum of all subarrays by brute force, then pointed the maxsum to end index of the sum array which holds the sum of longest subarray which array itself, then subtracted the indexes from maxsum to find the max sub array sum. plz help me .
#include
using namespace std;
int main()
{
int t;
cin >> t;
int currentsum, maxSum;
while (t–)
{
currentsum = 0;
maxSum = 0;
int n;
cin >> n;
int arr[n];
int s[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
s[0] = arr[0];
for (int i = 1; i < n; i++)
{
s[i] = arr[i] + s[i - 1];
}
maxSum = s[n - 1];
for (int i = 0; i < n; i++)
{
currentsum = currentsum + (s[n - 1] - s[i]);
if (currentsum > maxSum)
{
maxSum = currentsum;
}
}
cout << maxSum << endl;
}
return 0;
}