#include<stdio.h>
#define MAX(a,b) ((a>b)?(a):(b))
main()
{
int arr[]={-2, -3, 4, -1, -2, 1, 5, -3};
int len,i;
int max=0;
len = sizeof(arr)/sizeof(arr[0]);
max=arr[0];
int finalmax=0;
int start=0,end=0;
for(i=1;i<len;i++)
{
max = MAX(max+arr[i], arr[i]);
finalmax=MAX(max,finalmax);
}
printf("MAX:%d %d %d \n",finalmax, start ,end);
}
Example if you want to print array http://codepad.org/BhYxSlp4
3 comments :
the MAX in the if condition is useless.
@Avi , yeah thanks for informing , i missed to remove it , i have updated the post
Post a Comment