Find an array of integers and the largest contiguous subarray

Source: Internet
Author: User

Topic:

give any array of integers to find the largest contiguous subarray of this array (the Subarray and the largest and the sub-arrays are contiguous). Requirement: The time complexity of the algorithm is O (n).

Program Design Ideas :

1: Record the current contiguous subarray with MaxValue and the value for maximum and initialize its value to: maxvalue=a[0]. Note: The array is a[n].

2: The general idea of this process is that, starting from the beginning of the array header, each time a value is added, their sum is recorded as Tempvalue, and if Tempvalue is smaller than the newly added value itself, the value of Tempvalue should be recalculated starting from this position. And each time the tempvalue should be compared with MaxValue, if the Tempvalue greater, then the value of MaxValue should be updated to Tempvalue value, relative MaxValue corresponding sub-array will also be tempvalue corresponding sub-array. This way, the maximum and continuous subarray can be obtained and maxvalue after the array is fully scanned at the end. The time complexity is exactly O (n).

3: Execute for Loop: for (i=1;i<n;i++). The content of the loop body is: Use Tempvalue to record the current tempvalue with the next value in the array and even if: Tempvalue=tempvalue+a[i], initialize its value is: tempvalue=a[0]. After updating the value of Tempvalue, if Tempvalue is less than or equal to A[i], the current tempvalue corresponding sub-array should be discarded so that it corresponds to a new array, and the tempvalue corresponding sub-array should be pointed to a[i]. If Tempvalue is greater than a[i] and Tempvalue is greater than MaxValue, the corresponding sub-array of MaxValue is updated so that its corresponding sub-arrays become tempvalue corresponding sub-arrays. After the work is done, continue with the next cycle.

SOURCE program code:

ImportJava.util.Scanner; Public classSecond { Public Static voidMain (string[] args) {//TODO auto-generated Method StubScanner SC=NewScanner (system.in); System.out.println ("Input Array Length"); intn=Sc.nextint (); System.out.println ("Input array data (separated by spaces)"); inti; inta[]=New int[n];  for(i=0;i<n;i++) A[i]=Sc.nextint (); String begin= "" +0;//Sub-array start subscriptString end= "" +0;//Sub-array end subscript        intMaxvalue=a[0]; intTempvalue=MaxValue; //System.out.println (Max.length ());         for(i=1;i<n;i++) {Tempvalue+=A[i]; if((Tempvalue>a[i]) && (tempvalue>maxValue)) {End=""+i; MaxValue=Tempvalue; }                        Else if(tempvalue<=A[i]) {Begin=""+i; End=""+i; Tempvalue=A[i]; }} System.out.println ("Maximum sub-array and is:" +maxvalue+ "\ n sub-array contents are:"); System.out.println ("Subscript:");  for(I=integer.parseint (begin); I<=integer.parseint (end); i++) System.out.print (i+"  "); System.out.println ("\ n" + "subscript corresponding value:");  for(I=integer.parseint (begin); I<=integer.parseint (end); i++) System.out.print (A[i]+"  "); }}

Operation Result:

Find an array of integers and the largest contiguous subarray

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.