Why is the run time error is coming?

#include<bits/stdc++.h>
using namespace std;
int main(){

int n,key,k;
int test_case;
int a[1000]={0};
cin>>test_case;

while(test_case>0){

cin>>n;
int left=-1;
int right=-1;
int current_sum=0;
int max_sum=0;

for(int i=0;i<n;i++){
	cin>>a[i];
}

for(int i=0;i<n;i++){
for(int j=i;j<n;j++){

	current_sum=0;
	    for( int p=i;p<=j;p++){
		    // cout<<a[p]<<",";
		    current_sum=current_sum+a[p];
		    key=i;}
	    // cout<<current_sum<<",";
	    // cout<<endl;
	       if(current_sum>max_sum){
    	        max_sum=current_sum;
    	        left=i;
    	        right=j;
            }
            

}

}
cout<<max_sum<<endl;
// cout<<left<<endl;
// cout<<a[key];
// for(int s=left;s<=right;s++){
// cout<<a[s]<<",";
// }

test_case--;

}
return 0;

}

@Kshubham1532
the value of N:
1 <= N <= 100000
and you have initialised an array : int a[1000] = {0};
so you will get runtime error.
and also you have to do this in o(N) time complexity and not O(N*N)

sir what is the significance of int a[1000]

when you are taking input in array of size 1000 you can only take 1000 numbers as the input. but the numbers could be 100000 so you are getting seg fault.

oh right ,thank you so much sir!

But how can we know about the time complexity, i mean why only O(n) is used ,why not(n^2)?

because n^2 would give TLE. I suggest please read about time complexity, it would be really helpful. If you took an advance course then you can refer some videos on youtube.

1 Like

why Run error is coming?

doubt cleared sir THankyou