C
Programming and Technical
Programming
Program
Find out maximim sum of sub Array
example
array={2,3,-1,4,9}
maximum sum of sub array=17
Read Solution (Total 3)
-
- #include
#include
int maxSubArraySum(int a[], int size)
{
int max_so_far = 0, max_ending_here = 0;
int i;
for(i = 0; i < size; i++)
{
max_ending_here = max_ending_here + a[i];
if(max_ending_here < 0)
max_ending_here = 0;
if(max_so_far < max_ending_here)
max_so_far = max_ending_here;
}
return max_so_far;
}
/*Driver program to test maxSubArraySum*/
int main()
{
int a[] = {2,3,-1,4,9};
int n = sizeof(a)/sizeof(a[0]);
int max_sum = maxSubArraySum(a, n);
printf("Maximum contiguous sum is %dn", max_sum);
getchar();
return 0;
}
- 10 years agoHelpfull: Yes(1) No(0)
- an array should not equal to sub array so sub array can be formed as
B={2,3,-1,4}=2-1+3+4=8
B={3,-1,4,9}=3-1+4+9=15 similarly the maximum sum of sub array will be
B={3,4,2,9}=18 - 10 years agoHelpfull: Yes(0) No(0)
- The question is vague.
- 10 years agoHelpfull: Yes(0) No(0)
C Other Question