The and of the largest sub-arrays in a two-dimensional integer array

Source: Internet
Author: User

Title: Returns the maximum number of sub-arrays in a two-dimensional integer array

Requirements: N input a two-dimensional array of shaping, there are positive numbers in the array and negative numbers. N a contiguous sub-matrix in a two-dimensional array that consists of one sub-array, and each sub-array has one and. N to find the maximum value of the and of all sub-arrays. Requires a time complexity of O (n). Team member: Chen Chen (responsible for program analysis, code programming), Wang Yingrui (responsible for code review and Code test plan) ideas: two-dimensional array to find the largest sub-array is extended from a one-dimensional array, first determine the maximum upper bound in the case of the first row to determine the maximum range of sub-arrays, for the specified maximum Subarray range, The maximum number of sub-arrays in a column has several numbers, divided into several different rows, for the above-mentioned several arrays, a one-dimensional array of sub-arrays and the maximum value of the method. The maximum value is calculated in turn, and the maximum is preserved. is the maximum upper bound drop repeat above steps.

For the sum of the sub-arrays of the two-dimensional array, the idea and the difference between one-dimensional array is not much, but need to analyze more cases, I discussed with the roommate after the written. Here's my idea:

1. Determine the maximum upper bound of the subarray, descending from the first row, to specify the range of the largest subarray.

2. For the range of the specified maximum Subarray, the maximum subarray is divided into several different rows according to the number of a column. (If the first group has only one row, the second group has two rows, and the number of columns and columns is the same)

3. For a few arrays produced in the second step, a one-dimensional array is used to find the maximum value of the subarray. The maximum value is calculated in turn, and the maximum

4. Reduce the maximum upper bound by 1, re-2nd, 3, 4 steps.

Code:
#include <iostream>using namespace Std;void main () {int x,y,i,j,m=0,a[100][100];cout<< "input matrix rows () and columns"; CIN >>x>>y;if (x>100| | y>100) {cout<< "Please re-enter:"; cin>>x>>y;} for (i=0;i<x;i++) {for (j=0;j<y;j++) {cin>>a[i][j];}} int sum[100]={0},max=0,result=a[0][0];for (i=0;i<x;i++)//determines the maximum upper bound of the Subarray (for line i) {while (m+i<x)//determines that the subarray has a m+i row {// The handle array is the same as an array, the maximum Subarray and for (j=0;j<y;j++) {sum[j]=sum[j]+a[m+i][j];} Max=0;for (j=0;j<y;j++) {if (Max+sum[j]>sum[j]) {max=max+sum[j];} ELSE{MAX=SUM[J];} if (Max>result) {Result=max;}} m++;//is the number of rows of the Subarray +1}//the values of M and sum[], causing the maximum upper bound of the Subarray to fall by 1 and then re-loop. M=0;for (j=0;j<y;j++) {sum[j]=0;}} Cout<<result;}


Test:

The following tests were conducted for this program:

1. Full negative: The largest negative number in the output matrix

2. There are only 1 values: the output itself

3. All 0: Output 0

4. Only one row or column: Output normal

5. Out of array range: Re-enter (code limit)

Summary: The two-dimensional array to maximize the sub-array is extended from a one-dimensional array, one-dimensional array can be solved linearly, for the two-bit array of the original idea is to O (n) time complexity of the implementation can go through their own data and students to discuss the discussion or not come up with a solution, finally can only make the results as the The task is still pair development in this development process, I and Chen Chen students Division of Labor clearly also gradually find out some attention to development issues, in the work will have different views such as in the discussion of the development of ideas, We face the opinion not at the same time we each tell each other's own opinion in the analysis of who's idea is better this also solves the problem, the knot pair development has reaped a lot.

Work Photo:

The and of the largest sub-arrays in a two-dimensional integer 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.