I would like to find the maximal subsequence S(h,k)
in an array of integer. I already have the code (in Java) to find the maximum value and it works fine, but how can I get the two indexes h
and k
from the following?
int []a = {-2, 1, -3, 4, -1, 2, 1, -5, 4 };
int max_so_far = 0, max_ending_here = 0;
for(int i=0; i<a.length; i++) {
max_ending_here = Math.max(0, max_ending_here + a[i]);
max_so_far = Math.max(max_so_far, max_ending_here);
}
System.out.println("The maximal sum of subsequence is = "+max_so_far)";