Find the largest of the Subarray and "Microsoft interview 100 Third question"

Source: Internet
Author: User

Title Requirements:

Enter an integer array with integers in the array and negative numbers. One or more consecutive integers in an array make up a sub-array, each of which has a single and.

The maximum value for the and of all sub-arrays. Requires a time complexity of O (n).

For example: The input array is 1,-2,3,10,-4,7,2,-5, and the largest sub-array is 3,10,-4,7,2, so the output is the and 18 of the sub-array.

Reference: Sword refers to the 31st question, the beauty of programming 2.14.

Topic Analysis:

Read the array value in sequence, using two temporary variables

      • Maxsum: The maximum value of the sub-array before the current array element is saved;
      • Maxendinghere: The sum of a sub-array containing the previous element of the current element, if Maxendinghere is not positive, the update maxendinghere is the current element value after the current element is added, and if Maxendinghere is a positive number, After adding the current element, update maxendinghere = maxendinghere+ the current element value.

Extension issues:

    • Extension 1: If the array is adjacent, that is, we allow to find a number (A[i],..., a[n-1],a[0],..., a[j]), please make it and maximum, what to do?

Answer: Programming Beauty 2.14 Expansion problem, also can be seen this blog post-sequence programming beauty series.

Hint: Adopt three variables: max,min,sum;

Max: Sum of the largest subarray; min: Sums of the smallest sub-arrays; sum: total of the arrays

The last to take Max and Sum-min is to ask for the big person.

    • Extension 2: There is an integer sequence, with plus or minus and 0, where several consecutive numbers are summed, and the absolute value of the summation is the largest number string

   Answer: Programming Beauty 2.14 Expansion problem, also can be seen this blog post-sequence programming beauty series .

Tip: Compare the absolute value of the sum of the max and the smallest sub-arrays to the sum of min

    • Extension 3: The sum of the sub-arrays (two-dimensional case)

Answer: Programming Beauty 2.15 expansion problem, also can be seen this blog post-sequence programming beauty series .

#include <iostream>using namespacestd;intMaxsum (int*a,intn);intMainvoid){    inta[8] = {1,-2,3,Ten,-4,7,2,-5}; cout<< Maxsum (A,8); return 0;}intMaxsum (int*a,intN) {    intI,maxsum,maxendinghere; Maxsum= Maxendinghere = a[0];  for(i =1; i<n;i++)    {        if(maxendinghere<=0) Maxendinghere=A[i]; ElseMaxendinghere+=A[i]; if(maxendinghere>maxsum) Maxsum=Maxendinghere; }    returnmaxsum;}
View Code

  

   

Find the largest of the Subarray and "Microsoft interview 100 Third question"

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.