Returns the maximum number of sub-arrays in a one-dimensional integer array

Source: Internet
Author: User

Design idea: On the basis of the original largest array, the array is expanded first to twice times the original array is copied and then connected to the back. Then enter a number of times the loop, each cycle after the beginning of the scan to enter a number of numbers. Computes the array and continuously updates the maximum number combination

Program Source code:

 Packagetest;ImportJava.util.Scanner; Public classTest { Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); System.out.print ("Please enter the array length:"); intL=in.nextint ();//user Input Array length                int[] Shuzu =New int[L*2]; intNumber_hezuidazhi=0; System.out.println ("Please enter the number in turn");  for(inti=0;i<l;i++) {Shuzu[i]=In.nextint (); Shuzu[i+l]=Shuzu[i]; }                intNumber_he=shuzu[0];  for(intm=0;m<l;m++)//Enter a number of times to loop        {              for(intn=m;n<m+l;n++)//Number of digits to be scanned backwards each time{number_he= Number_he +Shuzu[n]; if(number_hezuidazhi<number_he)//if the array and is greater than the maximum array, the update{Number_hezuidazhi=number_he; }                            if(number_he<0)//if the array and is less than 0, then discard starts from zero{number_he=0; }} number_he= 0;//updates the array one time and} System.out.println ("Maximum array and for" +Number_hezuidazhi);    In.close (); }}

Results:

Result Analysis: Input-1 2 3-4 6

So in this end-to-end array, it should be discarded-4 of all the other numbers make up the largest set.

The program changes the array to-1 2 3-4 6-1 2 3-4 6

First Scan (-1 2 3-4 6)-1 2 3-4 6

Second Scan-1 (2 3-4 6-1) 2 3-4 6

。。。。

Is the algorithm of the last largest array each time

Summary: The time complexity of the program becomes O (n*n); there is no way to implement it once, and the array changes are traversed multiple times.

Returns the maximum number of sub-arrays in a one-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.