Find the largest sub-array of the unordered array (max + min)-for an interview question above the Learning Forum

Source: Internet
Author: User

Original question link:Http://www.51weixue.com/thread-398-1-1.html

Question:

Which of the interview questions of a company in Hangzhou can be guessed by yourself? It is not easy to say. It is a very difficult question, and very few questions can be made on the spot; it adds popularity to the Forum!

Returns the largest (max + min) Sub-array of an unordered array.

The question is to give an unordered array, all of which are positive numbers. You need to find a sub-array so that the minimum value and the maximum value in the sub-array can be obtained.
Example:3 9 2 7 1 5 8
The maximum sub-array (max + min) that can be obtained is (5, 8 ).

------------------------------- Split line ------------------------

A rough look, it is not difficult to come up with an algorithm with a time complexity of O (n ^ 2). It is nothing more than traversing all sub-arrays and finding out the one with the minimum value + the maximum value. But can we find an algorithm with the time complexity of O (n?

When I first thought about it, I did not really think of any simple method. I always thought about it in a complicated place. In fact, you only need to write several groups of sample data to find out the rule.

 

For example:

Example 1: 3 9 2 7 1 5 8, maximum (5, 8 ),

Example 2: 6 5 10 9 4 11, max (10, 9)

Example 3: 4 7 3 7 9 2 7, max (7, 9)

Example 4: 4 9 10 5 11 4 7 9, max (9, 10)

------------------------------

Do you see the rule? Have you found that the largest sub-array is composed of two elements? In this case, the child array that meets the conditions must be composed of two elements. Let's repeat it.

First, there are two elements at the beginning, one of which is the maximum value and the other is the minimum value. Then introduce the third element. If the third element is larger than the two elements, we can create a new sub-array that meets the conditions. For example, if the starting sub-array in example 2 is (6, 5), the maximum value is 6, and the minimum value is 5. After the third element 10 is introduced, the sub-array that meets the condition is (5, 10 ). Similarly, when the third element is shorter than the two elements, the largest subarray is composed of the two elements. For example, Example 3, (, 3 ).

Then, based on this rule, we can easily write an algorithm with a time complexity of O (n.

 

Code implementation:

# Include <stdio. h> const int N = 100; int A [N]; int FindMaxSum (int * ptrA, int n); int main (void) {int I, n; freopen ("in.txt", "r", stdin); while (scanf ("% d", & n )! = EOF) {for (I = 0; I <n; ++ I) {scanf ("% d", & A [I]);} printf ("% d \ n", FindMaxSum (A, n) ;}return 0 ;}int FindMaxSum (int * ptrA, int n) {int I, nTemp, nMaxSum = 0; for (I = 0; I <n; ++ I) {if (I + 1 <n) {nTemp = ptrA [I] + ptrA [I + 1]; if (nTemp> nMaxSum) {nMaxSum = nTemp ;}} if (1 = n) // only one element of the corresponding array exists. {nMaxSum = ptrA [0];} return nMaxSum ;}

 

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.