arr
and returns an array res
containing 3 integers in the following format:
res[0] = max sum
res[1] = starting index of the subsequence
res[2] = ending index of the subsequence
maxContSequence({-1,-2,3,4,5}) ==> {12,2,4}
maxContSequence({1,2,3,-2,5}) ==> {6,0,2}
max_sum
, representing the global maximum sum, curr_sum
representing the current temporary sum, start index
and the end index
of the array's elements representing the start and end indices of the contiguous subsequence whose sum will be maximum. current sum
is less than the value of the current element, make the current element the current sum
. The index of the current element becomes the start and end indices of the subsequence.current sum
and set the index of the current element as the ending index of the subsequence.current sum
is greater than max sum
update the max sum
with the current sum
and store the start index and end index in temporary variables which will be returned back along with the sum in the res
array.
public static int[] maxContSequence(int[] arr) { }
C
Java
Python