An important interview as well as optimised solution for finding the largest sub array sum in an array which may contain 0, positive and negative integers.
#include<stdio.h>
main()
{
int sumtillnow=0,sum=0,i=0;
int a[7]={-1,11,13,12,-90,23,5};
for(i=0;i<7;i++)
{
sumtillnow+=a[i];
if(sumtillnow>sum)
sum=sumtillnow;
if(sumtillnow<0)
sumtillnow=0;
}
printf("%d",sum);
}
#include<stdio.h>
main()
{
int sumtillnow=0,sum=0,i=0;
int a[7]={-1,11,13,12,-90,23,5};
for(i=0;i<7;i++)
{
sumtillnow+=a[i];
if(sumtillnow>sum)
sum=sumtillnow;
if(sumtillnow<0)
sumtillnow=0;
}
printf("%d",sum);
}
No comments:
Post a Comment