In the ancient one-dimensional pattern recognition, it is often necessary to calculate the maximum sum of continuous sub-vectors. When the vectors are all positive numbers, the problem is easy to solve. But if the vector contains negative numbers, should it contain some negative number and expect the positive number next to it to make up for it? For example: {6,-3,-2,7,-15,1,2,2}, the maximum sum of consecutive sub-vectors is 8 (starting from the 0th and ending at the 3rd)
code
public static int findMaxSumOfSubArray(int[] array) { if (array == null || array.length == 0) { throw new IllegalArgumentException("array is null or empty."); } if (array.length == 1) { return array[0]; } // Initialize the accumulated value and the maximum value as the first element of the array int sum = array[0]; int maxSum = array [0]; // traverse from the second element of the array for (int i = 1; i < array.length; i++) { // If the current accumulated value is negative, there is no need to accumulate it, because after adding the current element value, it will not be greater than the current element value anyway // So the accumulated value variable is updated to the current element value // If not negative, accumulate the current element value if (sum < 0) { sum = array[i]; } else { sum += array[i]; } // If the current accumulated value is greater than the maximum value, update the maximum value if (sum > maxSum) { maxSum = sum; } } return maxSum; } public static void main(String[] args) { int[] array = {6, -3, -2, 7, -15, 1, 2, 2}; int max = findMaxSumOfSubArray(array); System.out.println(max); }
Returns the maximum and corresponding contiguous subarrays of contiguous subarrays
public static int[] findSubArrayWithMaxSum(int[] array) { if (array == null || array.length == 0) { throw new IllegalArgumentException("array is null or empty."); } if (array.length == 1) { return array; } // Initialize the accumulated value and the maximum value as the first element of the array int sum = array[0]; int maxSum = array [0]; // Record the starting position of the calculated sum int start = 0; // Record the starting position corresponding to the current maximum value int maxStart = 0; // Record the termination position corresponding to the current maximum value int maxEnd = 0; // traverse from the second element of the array for (int i = 1; i < array.length; i++) { // If the current accumulated value is negative, there is no need to accumulate it, because after adding the current element value, it will not be greater than the current element value anyway // So update the accumulated value variable to the current element value, and update the starting position to the current element's position // If not negative, accumulate the current element value if (sum < 0) { sum = array[i]; start = i; } else { sum += array[i]; } // If the current accumulated value is greater than the maximum value, update the maximum value and update the start and end position corresponding to the maximum value if (sum > maxSum) { maxSum = sum; maxStart = start; maxEnd = i; } } int[] result = new int[maxEnd - maxStart + 1]; for (int i = maxStart; i <= maxEnd; i++) { result[i - maxStart] = array[i]; } return result; } public static void main(String[] args) { int[] array = {-3, 6, -3, -2, 7, -15, 1, 2, 2}; int[] result = findSubArrayWithMaxSum(array); // output is 6 -3 -2 7 for (int i : result) { System.out.print(i + " "); } }