Run- error in coding blocks challenges

I am working on problem “Maximum Subarray Sum” , The below code works fine for the test case on coding blocks ide , and even for some other dummy cases but when run on the code submission area, I got error as:

TESTCASE #1: timelimit (Time: 5.88 s)

Not sure what may be the problem is, below is my code:

#include <iostream>
using namespace std;
int main() {
int cases ; //no of cases
cin >> cases; // the value is 2
//int sums[cases] {0};

for(int i = 0; i < cases; i++){ // both the cases, first case has 4 items and second has 3 items
    int items;
    cin >> items;
    //vector<int> vect(8);  
    int arr[items] = {};
    for(int j = 0; j < items; j++){ // 4 items reading one by one
        cin >> arr[j] ;
    }
    int max_sum = -100000000;
    for(int k = 0; k < items; k++){ // for each iteation sum of sub array: four tiems for 1st , 3 times for second
        int sums[items] {0} ;
        
        for(int l = k; l < items; l++){
            sums[k] = sums[k] + arr[l];
            if(sums[k] >= max_sum){
                max_sum = sums[k];
            }
        }
    }
    cout << max_sum << endl;
    
}
return 0;

}

Hey, It is giving Time Limit Exceeded error because the constraints given in the problem are big and time complexity of your solution is O(N^2). So, to reduce your code’s time complexity you can use Kadane’s algorithm in this question.

1 Like

Thanks, Got it, I thought there is an issue with the code.