answersLogoWhite

0

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;

/* Do not compare for all elements. Compare only

when max_ending_here > 0 */

else if (max_so_far < max_ending_here)

max_so_far = max_ending_here;

}

return max_so_far;

}

Time Complexity: O(n)

Algorithmic Paradigm: Dynamic Programming

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

How do you calculate the average of a list of numbers?

If you want to calculate the average of a list of numbers, add the numbers together and divide it by the number of numbers.


How do you work out the range from a list of numbers?

To calculate the range from a list of numbers, first identify the maximum and minimum values in the list. Subtract the minimum value from the maximum value. The result is the range, which represents the difference between the highest and lowest values in the dataset. For example, if your numbers are 3, 7, and 5, the range would be 7 - 3 = 4.


What is the maximum sum that can be achieved by selecting non-adjacent elements from a given list of numbers?

To find the maximum sum by selecting non-adjacent elements from a list of numbers, you can use dynamic programming. Start by creating an array to store the maximum sum up to each element. Iterate through the list of numbers and for each element, calculate the maximum sum by either including the current element or excluding it. Keep track of the maximum sum achieved so far. At the end of the iteration, the final element in the array will contain the maximum sum that can be achieved by selecting non-adjacent elements.


Is the mean of a list of numbers the maximum minus the minimum?

No. The maximum minus the minimum is the range. The mean is the sum of all elements of the list divided by the size of the list.


What is the math meaning of the word mean?

The mean is when you calculate the sum of a list of numbers and then you didvide by the number of numbers in the list.


How do I find the maximum in a range of numbers?

Compare two numbers, reject the smaller one. Compare the number you are left with and the next one on your list. Keep going to the end. The number you are left with is the maximum.There are other methods.


How do you do average of a lot of numbers?

To find the average of a list of numbers, add the numbers and divide by the number of numbers in the list.


What is the ratio of prime numbers to composite numbers in this list 101112131415161718192021?

what is the ratio or prime numbers to composite numbers in this list/10,11,2,13,14,15,16,1,7,18,19,20,21


What word names in ordered of list of numbers?

An ordered list of numbers is a sequence


Order list of numbers is called?

Surprisingly, it is called an ordered list of numbers!


What is the formula for calculating average?

[sum of numbers on list] &divide; [amount of numbers in list]


How do you find the mode in list of numbers?

It is the number that occurs the most in the list of numbers