First, title: Returns the and of the largest subarray in an integer array. Requirements: Enter an array of shapes, with positive numbers in the array and negative values. One or more consecutive integers in an array make up a sub-array, each of which has a and. 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. Returns the position of the largest subarray at the same time. The maximum value for the and of all sub-arrays. Requires a time complexity of O (n). Second, the design thinking is to start with a linked list to achieve the end of the array, but because the list is not familiar with, then think of another algorithm: when the maximum value does not appear across the domain is the usual method of calculation to find out,
When a cross-domain occurs, since a[0] a[n-1] all appear in the solution
In
, the solution can be extended to the maximum,
That is, starting from a[0], scanning to the right, starting from a[n-1] to the left, both until a negative number is present, so that there is no negative in the middle, but the two ends are positive, but the whole sequence is selected. Third, the source code
Zhao Jiansong, Zhang Wendong
#include <iostream.h> #include <stdlib.h>/* * Now is the array for a ring structure, maximum continuous and how is it implemented? * 1. The simplest method: Array loop shift, and then solve the maximum continuous and the algorithm complexity O (n^2) * 2.s[i][l] = s[i][l-1]+a[i+l-1]; sum = max{s[i][l]},s[i][l]: The solution of continuous and * O (n) with the L-start length is: * 1. The solution does not occur across domains, i.e. a[0] ~ a[n-1] (original problem) * 2. Cross-domain situation a[n-1]~a[0] appears in the middle of the solution or boundary */int max_subarray_bound (int a[],int from,int to) {int i,max,sum; max = A[from]; sum = A[from]; for (i = from + 1;i <= to;i++) {if (sum < 0) {sum = 0;} Sum + = A[i]; if (Max < sum) {max = sum;} } return max; int max_subarray_cycle (int a[],int size) {int sum,i,j,s,m; In the first case, no cross-domain sum = max_subarray_bound (a,0,size-1); /* * In the second case, the cross-domain, since a[0] a[n-1] all appear in the solution, you can extend its solution to the maximum, * that is, starting from a[0], to the right to scan to the left, starting from the a[n-1], the two until a negative number, * so there is no negative middle, but the two ends are positive, But the whole sequence is selected. */if (size = = 2) return sum; i = 1; j = size-2; s = a[i-1] + a[j+1]; while (I < J && (A[i] >=0 | | a[j] >= 0)) {if (A[i] >= 0) {s + = a[I]; i++; } if (A[j] >= 0) {s + = a[j]; j--; }} if (i = = j) {if (A[i] >0) s+=a[i]; if (s < sum) return sum; else return s; } if (I < J) {//Calculates all successive sub-arrays with I as the head, or continuous subarray with J as the tail, and finds the largest element int s2,k; m = A[i]; s2 = m; For (k=i+1;k <= j;k++) {s2 + = a[k]; if (M < s2) m = s2; } s2 = a[j]; for (k = j-1;k>=i;k--) {s2 + = a[j]; if (M < s2) m = s2; } if (M > 0) s + = m; } if (s < sum) return sum; else return s;} int main (int argc,char *argv[]) {int a[] = {5,-3,-1,5,10,11};for (int j=0;j<6;j++) {cout<<a[j]<< "";} cout<<endl<< "Maximum sub-array and as:" <<max_subarray_cycle (A,sizeof (a)/sizeof (a[0]) <<endl; return 0;}
Four
Five, experience
In the implementation of the ring array to maximize the sub-array of the process, I and Zhang Wendong think of a different way to implement the problem of the ring array, but there are shortcomings and not easy to implement the place, and finally found this method, but due to time constraints, the implementation of the function is not particularly perfect, But in the process of discussing various methods also divergent our thinking, have a harvest.
Software Engineering--pair Development ring Array