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 program code
PackageCom.minirisoft;ImportJava.util.*; Public classMaxarray { Public Static voidMain (string[] args) {intNum,i; intSum=0; intMax; Scanner Cin=NewScanner (system.in); System.out.print ("Please enter the length of the array:"); Num=Cin.nextint (); intarray[]=New int[num]; intMax1=array[0]; intMin=array[0]; for(i=0;i<num;i++) {Array[i]=Cin.nextint (); } for(i=0;i<num;i++) { if(sum<=0) {sum=Array[i]; } Else{sum=sum+Array[i]; } if(sum>max1) {Max1=sum; }} System.out.println ("Maximum value of the first case:" +max1); for(i=0;i<num;i++) { if(sum>=0) {sum=Array[i]; } Else{sum=sum+Array[i]; } if(sum<min) {min=sum; } } intSum1=0; for(i=0;i<num;i++) {sum1=sum1+Array[i]; } intMax2=sum1-min; System.out.println ("Maximum value of the second case:" +max2); if(max1>max2) {Max=Max1; } Else{Max=Max2; } System.out.println ("The maximum number of sub-arrays and is:" +max); }}
Third, the procedure
Iv. Experience
Regarding the design idea, we in the class discussion the idea and the classmate thought also is similar, is simply seeks the maximal subarray the sum, later we in the writing procedure process, the time complexity does not meet the request, has added one kind of idea, namely asks "sum-smallest subarray and", The sum of the maximum number of words in the two cases is the same as that of the non-circular array, and it is simpler to implement the array of gods and horses.
The return sub-array is not implemented now ...
Pair development--a ring-shaped one-dimensional array to find the maximum sub-array and