Code is not giving output as expected

#include
using namespace std;
int main(){
int n;
cin>>n;
int arr[n];

int cuSum[10] = {0};


cout<<"arr[0]"<<endl;
cin>>arr[0];
cuSum[0]=arr[0];

for(int i=1 ; i<n ; i++){
    cin>>arr[i];
    cuSum[i]=cuSum[i-1]+arr[i];
}

cout<<"Current Sum array is : ";
for(int i=0 ; i<n ; i++){
    cout<<cuSum[i]<<" ";
}


int left = -1;
int right = -1;
int sum = INT16_MIN;
for(int i=0 ; i<n ; i++){
    for(int j=i ; j<n ; j++){
        int currSum = 0;
        currSum = cuSum[i-1] - cuSum[j];
        if(currSum > sum){
            sum = currSum;
            left = i;
            right = j;
        }
    }
}
cout<<sum<<endl;
for(int i=left ; i<=right ; i++){
    cout<<arr[i]<<" ";
}
cout<<endl;
cout<<"left "<<left<<" right "<<" "<<right;

return 0;

}

Can you give the tc for which it is failing?

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.

Sir I told you the code is not giving output as expected, and I tried your solution but that is not working as well. Can you please help to to identify my error in the code. Thank you

could you send me your updated code.


update your code a little look for the comments for more info.

Sir its working for most of the cases , but if you take -1,-2,-3,-4,-5 then it is not giving proper output.

could you send me the problem link?

@debojyoti.koley.dk your code is trying to access cuSum[i-1] for i=0 .you should start your array from i=1 to 5 and keep a dummy index 0 with cuSum[0] = 0, then you can access cuSUm[i-1] even for first index.

else in my opinion it is best to use Kadane’s algorithm for finding max sum subarray.

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.