Programmer -- seek the largest sum of sub-Arrays

Source: Internet
Author: User

Prelude

I hope that more people will treat any interview questions in this crazy comedy series as a simple programming question or a substantive question like me, in the course of reading this crazy series, I hope you can put all the burdens related to the interview as much as possible, and devote yourself to solving every "programming question". In the process of solving programming questions, enjoy the unlimited fun of programming and the unlimited passion of thinking.


Section 1: Calculate the largest sum of sub-Arrays
3. Obtain the largest sum of sub-Arrays
Description:
Enter an integer array, and the array contains positive and negative numbers.
One or more consecutive integers in the array form a sub-array. Each sub-array has a sum.
Returns the maximum value of the sum of all sub-arrays. The time complexity is O (n ).

For example, the input array is 1,-2, 3, 10,-4, 7, 2,-5, and the maximum sub-array is 3, 10,-4, 7, 2,
Therefore, the output is the sum of 18 of the sub-array.

Analysis: this problem occurs frequently during interviews with major companies and is cited many times. Non-General interview questions can be matched. With this alone, there is no reason not to be included in the fantasy series. This was one of the 100 questions of Microsoft's 3rd questions that I have previously compiled. So far, this question has received a lot of attention. OK. Next, let's analyze this question step by step:
1. Obtain the largest sub-array and of an array, so that the sequence 1,-2, 3, 10,-4, 7, 2,-5, I think the most intuitive and brutal way is to traverse three layers of the for Loop, find the sum of each sub-array in the array, and finally find the maximum value of these sub-arrays.
Sum [I ,..., J] represents the Sum (0 <= I <= j <n) between the I-th element and the j-th element in array A, traversing all possible Sum [I ,..., J], so the time complexity is O (N ^ 3 ):

// This code is introduced from the beauty of Programming
Int MaxSum (int * A, int n)
{
Int maximum =-INF;
Int sum;
For (int I = 0; I <n; I ++)
{
For (int j = I; j <n; j ++)
{
For (int k = I; k <= j; k ++)
{
Sum + = A [k];
}
If (sum> maximum)
Maximum = sum;
}
}
Return maximum;
}

2. In fact, the following O (N) algorithm is directly provided for the Microsoft 100 Question and Answer V0.2 [answer to Question 1-20] I uploaded earlier: view plaincopy to clipboardprint?
// Copyright @ July 2010/10/18
// Updated, 2011.05.25.
# Include <iostream. h>

Int maxSum (int * a, int n)
{
Int sum = 0;
// In fact, it is very easy to handle all negative numbers. As you can see at, change this sentence to "int sum = a [0 ]".
// You can also leave it unchanged. If all values are negative, 0 is returned directly, which is not necessarily the case.
Int B = 0;

For (int I = 0; I <n; I ++)
{
If (B <= 0) // change B <0 to B <= 0
B = a [I];
Else
B + = a [I];
If (sum <B)
Sum = B;
}
Return sum;
}

Int main ()
{
Int a [10] = {1,-2, 3, 10,-4, 7, 2,-5 };
// Int a [] = {-1,-2,-3,-4}; // use case where all tests are negative.
Cout <maxSum (a, 8) <endl;
Return 0;
}

/*-------------------------------------
Explanation:
For example, the input array is 1,-2, 3, 10,-4, 7, 2,-5,
The largest sub-array is 3, 10,-4, 7, 2,
Therefore, the output is the sum of 18 of the sub-array.
 
Everything is in the following two lines,
That is:
B: 0 1-1 3 13 9 16 18 7
Sum: 0 1 1 3 13 13 16 18 18

In fact, the algorithm is very simple. After adding up the current number, after B <0,
Assign B a value again and set it to the next element, B = a [I].
When B> sum, sum = B is updated;
If B <sum, sum is the original value and is not updated .. July and 10/31.
----------------------------------*/
// Copyright @ July 2010/10/18
// Updated, 2011.05.25.
# Include <iostream. h>

Int maxSum (int * a, int n)
{
Int sum = 0;
// In fact, it is very easy to handle all negative numbers. As you can see at, change this sentence to "int sum = a [0 ]".
// You can also leave it unchanged. If all values are negative, 0 is returned directly, which is not necessarily the case.
Int B = 0;
 
For (int I = 0; I <n; I ++)
{
If (B <= 0) // change B <0 to B <= 0
B = a [I];
Else
B + = a [I];
If (sum <B)
Sum = B;
}
Return sum;
}

Int main ()
{
Int a [10] = {1,-2, 3, 10,-4, 7, 2,-5 };
// Int a [] = {-1,-2,-3,-4}; // use case where all tests are negative.
Cout <maxSum (a, 8) <endl;
Return 0;
}

/*-------------------------------------
Explanation:
For example, the input array is 1,-2, 3, 10,-4, 7, 2,-5,
The largest sub-array is 3, 10,-4, 7, 2,
Therefore, the output is the sum of 18 of the sub-array.

Everything is in the following two lines,
That is:
B: 0 1-1 3 13 9 16 18 7
Sum: 0 1 1 3 13 13 16 18 18

In fact, the algorithm is very simple. After adding up the current number, after B <0,
Assign B a value again and set it to the next element, B = a [I].
When B> sum, sum = B is updated;
If B <sum, sum is the original value and is not updated .. July and 10/31.
----------------------------------*/

3. After seeing the answer above, many of my friends thought that the code of idea 2 above did not handle all negative numbers. When it was all negative, we could let the program return 0, you can also let it return the largest negative number. The following code is rewritten in the previous few days. The modified code is used to handle all negative values (return the largest negative number:

View plaincopy to clipboardprint?
// Copyright @ July
// July, updated, 2011.05.25.
# Include <iostream. h>
# Define n 4 // define a variable

Int maxsum (int a [n])
// Here, you can see the advantages of the above idea 2 code (pointer)
{
Int max = a [0]; // The maximum number of returned values.
Int sum = 0;
For (int j = 0; j <n; j ++)
{
If (sum> = 0) // if sum> = 0 is added, add
Sum + = a [j];
Else
Sum = a [j]; // If sum is added, sum <0 will not be added.
If (sum> max)
Max = sum;
}
Return max;
}

Int main ()
{
Int a [] = {-1,-2,-3,-4 };
Cout <maxsum (a) <endl;
Return 0;
}
// Copyright @ July
// July, updated, 2011.05.25.
# Include <iostream. h>
# Define n 4 // define a variable

Int maxsum (int a [n])
// Here, you can see the above idea 2 code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.