Java Array Max Value maxSum(int[] array)

Here you can find the source of maxSum(int[] array)

Description

max Sum

License

Open Source License

Declaration

public static int maxSum(int[] array) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {

    public static int maxSum(int[] array) {
        if (array == null || array.length == 0) {
            return 0;
        }/*  w  w  w .j a  v  a 2s. c o m*/

        int maxEndIndex = 0;
        int maxSum = 0;
        int length = array.length;
        for (int i = 0; i < length; i++) {
            maxEndIndex = Math.max(maxEndIndex + array[i], 0);
            maxSum = Math.max(maxSum, maxEndIndex);
        }

        return maxSum;
    }

    public static long maxSum(long[] array) {
        if (array == null || array.length == 0) {
            return 0;
        }

        long maxEndIndex = 0;
        long maxSum = 0;
        int length = array.length;
        for (int i = 0; i < length; i++) {
            maxEndIndex = Math.max(maxEndIndex + array[i], 0);
            maxSum = Math.max(maxSum, maxEndIndex);
        }

        return maxSum;
    }

    public static float maxSum(float[] array) {
        if (array == null || array.length == 0) {
            return 0;
        }

        float maxEndIndex = 0;
        float maxSum = 0;
        int length = array.length;
        for (int i = 0; i < length; i++) {
            maxEndIndex = Math.max(maxEndIndex + array[i], 0);
            maxSum = Math.max(maxSum, maxEndIndex);
        }

        return maxSum;
    }

    public static double maxSum(double[] array) {
        if (array == null || array.length == 0) {
            return 0;
        }

        double maxEndIndex = 0;
        double maxSum = 0;
        int length = array.length;
        for (int i = 0; i < length; i++) {
            maxEndIndex = Math.max(maxEndIndex + array[i], 0);
            maxSum = Math.max(maxSum, maxEndIndex);
        }

        return maxSum;
    }
}

Related

  1. maxRow(Object[]... arr)
  2. maxRowLen(Object[]... arr)
  3. maxSize(String[] string)
  4. maxSort(double[] arr)
  5. maxStartFromIndex(double[] array, int startFromIndex)
  6. maxValue(double[] ary)
  7. maxValue(double[] from)
  8. maxValue(final int[] arr)
  9. maxValue(float[] arr)