Java Implementation: obtain the largest sum (array) of the sub-array and the java Array

Source: Internet
Author: User

Java Implementation: obtain the largest sum (array) of the sub-array and the java Array

Returns the largest sum of sub-arrays (arrays)

Question:
Enter an integer array, and the array contains positive and negative numbers. One or more consecutive integers in the array form a sub-array. Each sub-array has a sum. Returns the maximum value of the sum of all sub-arrays. The time complexity is O (n ).

For example, the input array is 1,-2, 3, 10,-4, 7, 2,-5, and the maximum sub-array is 3, 10,-4, 7, 2, so the output is the sum of 18 of the Child array.


Solution:

I think there are two types of data that users care about: one is the sum of the largest sub-data and the specific value, and the other is the specific elements of the sub-data, so we can encapsulate a sub-data class SubArray, the member has an array to start the index, end the index, and the sum of sub-data.


For this question, when I read other blog answers, I input an input parameter with the length and size of the sub-array. All the input parameters with the length are the maximum values of the size sub-array. However, there is no description about the size input parameter in the above questions. We still provide solutions for these two methods.

First, input the specified array length size. This method is easy to implement. First, we have a maximum sub-array SubArray variable maxArray, And then we initialize a SubArray object with the size, named currentArray. When we traverse a user element, we first remove the first element from currentArray and then put it into the user element. When the sum of the new currentArray is greater than that of maxArray, we can update maxArray.

Second, the array length size is not input. We only need to ensure that the sum of the data before the index is added with the index data greater than 0, then the maximum value of the index is contributed. When the sum of the data before the index plus the sum of the index <0, it means that the data before the index (including the index) is useless and can be deleted. When we get the above array, we need to repeat it in reverse order to remove the negative value at the end of the team to ensure that the sum of the sub-array is the largest.

The Code is as follows:

Public class MaxSubArray {private List <Integer> valueList = null; public MaxSubArray () {valueList = new ArrayList <Integer> () ;}public MaxSubArray (int size) {valueList = new ArrayList <Integer> (size);} public void add (int value) {valueList. add (value);} public SubArray maxSubArray (int size) {if (valueList. isEmpty () {return null;} // initializes the array of the current operation and the maximum array int currentSize = Math. min (valueList. size (), size); SubArray cur Arrays array = new SubArray (0, 0, 0); for (int I = 0; I <currentSize; I ++) {currentArray. endIx = I; currentArray. sum + = valueList. get (I) ;}subarray maxArray = new SubArray (currentArray); // calculate for (int I = size; I <valueList. size (); I ++) {// if no blank space is reserved for the current array, the element is removed from the beginning to ensure that one empty space is reserved if (currentArray. endIx-currentArray. startIx + 1> = size) {currentArray. sum-= valueList. get (currentArray. startIx); currentArray. startIx ++;} CurrentArray. endIx = I; currentArray. sum + = valueList. get (I); if (currentArray. sum> maxArray. sum) {maxArray. copy (currentArray) ;}}return maxArray;} public SubArray maxSubArray () {if (valueList. isEmpty () {return null;} // loop from the past to the back, as long as the sum before the I position is smaller than the I position value, the Child array including the I position is discarded. SubArray lastMaxArray = new SubArray (0, 0, valueList. get (0); SubArray resultArray = new SubArray (lastMaxArray); for (int I = 1; I <valueList. size (); I ++) {int value = valueList. get (I); if (resultArray = null) {resultArray = new SubArray (I, I, value); continue;} if (resultArray. sum + value <0) {if (resultArray. sum> lastMaxArray. sum) {lastMaxArray. copy (resultArray);} resultArray = null; continue;} resultArr Ay. endIx = I; resultArray. sum + = valueList. get (I);} // a forward loop from the back. If the sum after the I position is smaller than the I position value, the rear array, including the I position, is discarded. For (int I = resultArray. endIx; I> = resultArray. startIx; I --) {int value = valueList. get (I); if (value <0) {resultArray. endIx = I-1; resultArray. sum-= value; continue;} else {break;} return resultArray;} public class SubArray {private int startIx; private int endIx; private long sum; public SubArray (SubArray copy) {copy (copy);} public SubArray (int startIx, int endIx, long sum) {this. startIx = startIx; this. endIx = endIx; this. sum = sum;} public void copy (SubArray copy) {this. startIx = copy. startIx; this. endIx = copy. endIx; this. sum = copy. sum;} public int getStartIx () {return this. startIx;} public int getEndIx () {return this. endIx;} public long getSum () {return this. sum ;}}}

Junit Test


Public class MaxSubArrayTest {@ Testpublic void testMaxOneSizeArray () {Random random = new Random (); int count = 100000; int maxValue = 0; int maxValueIx = 0; maxSubArray sub = new MaxSubArray (count); for (int I = 0; I <count; I ++) {int value = random. nextInt (); if (value> maxValue) {maxValue = value; maxValueIx = I;} sub. add (value);} SubArray maxArray = sub. maxSubArray (1); assertEquals (maxArray. getStartIx (), maxArray. getEndIx (); assertEquals (maxArray. getStartIx (), maxValueIx); assertEquals (maxArray. getSum (), maxValue) ;}@ Testpublic void testMaxSizeArray () {int [] valueArr = {1,-2, 3, 10,-4, 7, 2, -5}; int count = valueArr. length; MaxSubArray sub = new MaxSubArray (count); for (int I = 0; I <count; I ++) {sub. add (valueArr [I]);} SubArray maxArray = sub. maxSubArray (5); // The maximum number of 5 word groups is 3, 10,-4, 7, 2 assertEquals (maxArray. getStartIx (), 2); assertEquals (maxArray. getEndIx (), 6); assertEquals (maxArray. getSum (), 18) ;}@ Testpublic void testMultiMaxSizeArray () {Random random = new Random (); int count = 100; int size = random. nextInt (count/2); while (size <= 0) {size = random. nextInt (count/2);} long maxValue = 0; MaxSubArray sub = new MaxSubArray (count); int maxValueStartIx = count/2-size; int I = 0; (; I <maxValueStartIx; I ++) {int value = random. nextInt (1000); sub. add (value) ;}int maxValueEndIx = maxValueStartIx + size-1; for (; I <= maxValueEndIx; I ++) {int value = random. nextInt (10*1000); while (value <1*1000) {value = random. nextInt ();} sub. add (value); maxValue + = value;} for (; I <count; I ++) {int value = random. nextInt (1000); sub. add (value);} SubArray maxArray = sub. maxSubArray (size); assertEquals (maxArray. getStartIx (), maxValueStartIx); assertEquals (maxArray. getEndIx (), maxValueEndIx); assertEquals (maxArray. getSum (), maxValue) ;}@ Testpublic void testOneMaxArray () {}@ Testpublic void testMaxArray () {int [] valueArr = {1,-2, 3, 10, -4, 7, 2,-5, 6}; int count = valueArr. length; MaxSubArray sub = new MaxSubArray (count); for (int I = 0; I <count; I ++) {sub. add (valueArr [I]);} SubArray maxArray = sub. maxSubArray (); // The maximum sub-array is 3, 10,-4, 7, 2 assertEquals (maxArray. getStartIx (), 2); assertEquals (maxArray. getEndIx (), 8); assertEquals (maxArray. getSum (), 19 );}}



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.