The largest and most large sub-arrays of an array

Source: Internet
Author: User

Title: The maximum and the number of sub-arrays of an array, which requires O (n) time complexity.

Because of the O (n) time complexity limit, the O (n^2) method of brute force solution is certainly not. Consider recursively the maximum of the subarray of an array a[n], which can be decomposed to the maximum of the a[i] sub-array, and to a condition between a[n-i-1].

    • A[n] The maximum sum of the subarray maximum and equal to the A[i] sub-array;
    • A[n] Sub-array max and equals a[n-i-1];
    • A[n] Sub-arrays Max and cross A[i] and a[n-i-1];

The time complexity of the recursive implementation is O (NLG (n)). Finally, the implementation of dynamic programming with time complexity of O (n) is considered.

/*** Created by Elvalad on 2014/12/4. * To find the maximum and number of sub-arrays of an array*/Importjava.util.ArrayList;ImportJava.util.Scanner; Public classMaxsubarray {/*violent traversal of all sub-arrays and*/     Public Static intMaxSubArray1 (arraylist<integer>a) {intsum = 0; intMax =Integer.min_value;  for(inti = 0; I < a.size (); i++) {sum= 0;  for(intj = i; J < A.size (); J + +) {sum+=A.get (j); if(Sum >max) {Max=sum; }            }        }        returnMax; }    /*Recursive Implementation*/     Public Static intMaxSubArray2 (Arraylist<integer> A,intLowintHigh ) {        intMax =Integer.min_value; intMax1 =Integer.min_value; intMAX2 =Integer.min_value; intMax3 =Integer.min_value; intMAX4 =Integer.min_value; intsum = 0; if(Low <High ) {            intMID = low + (high-low)/2; /*Maximum sub- array maximum for the left half of the Subarray*/Max1=MaxSubArray2 (A, low, mid); /*Maximum sub- array maximum for the right half of the Subarray*/Max2= MaxSubArray2 (A, Mid + 1, high); /*the maximum sub-array spans the mid element, so the maximum value must be the maximum of mid-left plus the maximum of mid-to-right*/             for(inti = mid; I >= low; i--) {sum+=A.get (i); if(Sum >max3) {Max3=sum; }} sum= 0;  for(inti = mid + 1; I <= high; i++) {sum+=A.get (i); if(Sum >max4) {max4=sum; }            }            returnMath.max (Math.max (Max1, Max2), (Max3 +max4)); } Else {            returnA.get (0); }    }    /*Dynamic Planning*/     Public Static intMaxSubArray3 (arraylist<integer>a) {intMax =Integer.min_value; intsum = 0; /*Each element updates the value of sum, zeroing when sum<0, and updating Max if Sum>max*/         for(inti = 0; I < a.size (); i++) {sum+=A.get (i); if(Sum >max) {Max=sum; } Else if(Sum < 0) {sum= 0; }        }        returnMax; }     Public Static voidMain (string[] args) {Scanner scan=NewScanner (system.in); ArrayList<Integer> array =NewArraylist<integer>();  while(Scan.hasnext ()) {Array.add (Scan.nextint ()); } System.out.println ("The Max Sub array is:" +maxSubArray1 (array)); System.out.println ("The Max Sub array is:" + maxSubArray2 (array, 0, Array.size ()-1)); System.out.println ("The Max Sub array is:" +maxSubArray3 (array)); }}

The largest and most large sub-arrays of an 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.