c++outputkadanes-algorithm

Why this code is returning wrong value? inputs= 5 ,{ -1,4,-6,7,-4}


#include<iostream>
#include<climits>
using namespace std;
int main(){
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;++i){
        cin>>a[n];
    }                                       //array instillisation
    int cursum=0;
    int maxsum=INT_MIN;
    for(int i=0;i<n;++i){
             cursum+=a[i];
             if(cursum<0){
                cursum=0;
             }
             maxsum=max(cursum,maxsum);
                }
      cout<<maxsum<<endl;               
      return 0; 
}

//this code is for maximum subarray problem using kadane's algo.My compiler is retrurning wrong output


Solution

  • In cin you are doing incorrect operation it should be

     cin>> a[i];
    

    what you are doing is taking the value of a[n]