Returns the maximum number of sub-arrays in an integer array and

Source: Internet
Author: User

One: Design ideas

First define an empty array with the same length as the original array, then assign the first number of the original array to the first position of the empty array, and then use the loop to compare the number of the original array with the previous number of the empty array for negative numbers. By comparing the second array, we can get the largest subarray in the integer array and the time complexity is O (n).

Two: Source code

//returns the and of the largest sub-array in an integer array. //Letter 1405-1 class 20142966 Huang WeipengImportJava.util.Scanner; Public classShuzu { Public Static voidMain (string[] args) {//TODO Auto-generated method stubs        inti,length; System.out.println ("Please enter the number of numbers to compare:"); Scanner in=NewScanner (system.in); //Define Array LengthLength=In.nextint (); intlist[]=New int[length]; System.out.println ("Please enter the number you want to compare:"); Scanner T=NewScanner (system.in); //defining the contents of an array         for(i=0;i<length;i++) {List[i]=T.nextint (); }        //to define an empty storage array        intsize[]=New int[length]; //Add the numbers in the array to find the maximum valueSize[0]=list[0];  for(i=1;i<length;i++)        {            if(size[i-1]<0) {Size[i]=List[i]; }            Else{Size[i]=list[i]+size[i-1]; }        }        //find the maximum value         for(i=0;i<length-1;i++)        {            if(size[0]<size[i+1]) {size[0]=size[i+1]; }        }        //Maximum output valueSystem.out.println (size[0]); }}

Three: Program results

Returns the maximum number of sub-arrays in an integer array and

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.