Java Array Max Value max_val_subsequence(int[] vals)

Here you can find the source of max_val_subsequence(int[] vals)

Description

Returns the sum and bounds of the maximum subsequence sum within bounds returned in the form {sum, start_index, end_index};

License

Apache License

Declaration

static int[] max_val_subsequence(int[] vals) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from  w w w . java 2  s  . c om*/
     * Returns the sum and bounds of the maximum subsequence sum within bounds
     * returned in the form {sum, start_index, end_index};
     */
    static int[] max_val_subsequence(int[] vals) {
        int a = 0, b = 1, max = Integer.MIN_VALUE;
        int i = 0, sum = 0;
        for (int j = 0; j < vals.length; ++j) {
            sum += vals[j];
            if (max < sum) {
                max = sum;
                a = i;
                b = j + 1;
            }
            if (sum <= 0) {
                sum = 0;
                i = j + 1;
            }
        }
        return new int[] { max, a, b };
    }
}

Related

  1. max(T... values)
  2. max(T[] args)
  3. max_array(byte[] arr)
  4. max_index(int[] v)
  5. max_of_ints(int[] data)
  6. maxAbs(double[] a, int begin, int end)
  7. maxAbs(float[] a, int off, int length)
  8. maxarr(double[] a)
  9. maxArray(double[] input)