Maximum Subarray


  • 0
    S

    #include<iostream.h>
    int maxsubarray(int a[],size)
    {
    int max_so_far=a[0];
    int curr_max=a[0];

    for(i=1;i<size;i++)
    {
         curr_max=max(a[i],curr_max+a[i]);
         max_so_far = max(max_so_far, curr_max);
    

    }
    return max_so_far;
    }
    //directory prog
    int main()
    {
    int a[] = {-2, -3, 4, -1, -2, 1, 5, -3,8};
    int size=sizeof(a)/sizeof(a[0]);
    int maxsum=maxsubarray(a,n);
    cout << "maxsubarray is " << maxsum;
    return 0;
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.