Pair development--the and of the maximal subarray of a ring-shaped one-dimensional array

Source: Internet
Author: User

Title: Returns the and of the largest sub-array in an integer array.
Requirements:
(1) Enter an array of shapes with positive and negative numbers in the array.
(2) One or more consecutive integers in the array make up a sub-array, each of which has a and.
(3) If the array a[0] ... A[j-1] next to each other, allowing a[i-1], ... A[n-1], a[0] ... A[J-1] and the largest.
(4) Returns the position of the maximum subarray at the same time.
(5) The maximum value of the and of all sub-arrays is evaluated. Requires a time complexity of O (n).
First, design ideas
The optimal solution to this problem must be the following two possibilities.
Perhaps one: the optimal solution does not span array[n-1] to array[0], which is the same as a non-annular array.
Possible two: The optimal solution spans array[n-1] to array[0], a new problem.
For the first case, we can be based on the non-circular array to seek, for MAX1, for the second case, the original problem can be converted to the smallest sub-array and the problem, and then the array of all elements and subtract the smallest sub-array and, then the result must be across a[n-1] to a[0] case the largest sub-array and, Set to Max2. The final result is the larger of the MAX1 and MAX2.
Example 1: There are arrays 5,-1,-6, 8, 2
When the max1=10,max2=15 is obtained, the larger max2 is taken as the result.
Example 2: There are arrays-6, 8, 1, 6, 1
When the max1=15,max2=14 is obtained, the larger max1 is taken as the result.

Second, the source program

1  PackageCom.java.lianxi;2 ImportJava.util.*;3  Public classLianxi3 {4      Public Static voidMain (string[] args)5     {6         intnum,i;7         intSum=0;8         intMax;9Scanner cin=NewScanner (system.in);TenSystem.out.print ("Please enter the length of the array:"); Onenum=cin.nextint (); A         intarray[]=New int[num]; -         intMax1=array[0]; -         intMin=array[0]; the          for(i=0;i<num;i++) -         { -array[i]=cin.nextint (); -         } +          for(i=0;i<num;i++) -         { +              if(sum<=0) A              { atsum=Array[i];  -              } -              Else -              { -sum=sum+Array[i]; -              } in              if(sum>max1) -              { tomax1=sum; +              } -         } theSYSTEM.OUT.PRINTLN ("Maximum value of the first case:" +max1); *          for(i=0;i<num;i++) $         {Panax Notoginseng              if(sum>=0) -              { thesum=Array[i]; +                  A              } the              Else +              { -sum=sum+Array[i]; $                 $              } -              if(sum<min) -              { themin=sum; -              }Wuyi         } the         intSum1=0; -          for(i=0;i<num;i++) Wu         { -sum1=sum1+Array[i]; About         } $         intMax2=sum1-min; -SYSTEM.OUT.PRINTLN ("Maximum value of the second case:" +max2); -         if(max1>max2) -         { Amax=max1; +         } the         Else -         { $max=Max2; the         } theSystem.out.println ("sub-array and maximum value:" +max); the     } the  -}

Third, the operation result:

Iv. Experience

The first is the design idea, at the beginning we in class and the students think of the same, is simply the sum of the largest sub-array, and then we write the process, the time complexity does not meet the requirements, a new way of thinking, that is, "sum-minimum sub-array and". The sum of the maximum number of words in both cases is the same as the previous non-circular array summation idea, the realization of the proportion of the simple array of gods and horses, and now has not implemented the return sub-array, ...

Pair development--the and of the maximal subarray of a ring-shaped one-dimensional array

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.