Best stretch ever
Inputs
values is a list of 1 to 14 whole numbers, each from -9 to 9.
Your tasks
- Set
currentandbestboth to the first number. - Use a loop over the other numbers. For each number
x, setcurrentto the larger ofxalone andcurrent + x. Updatebestifcurrentis larger. - Complete
maxStretchSumso it returnsbest, the largest sum of one or more neighbouring numbers. Return it, do not print it.
Example
With values = [-2, 1, -3, 4, -1, 2, 1, -5, 4], maxStretchSum returns 6.
Rules
valuesalways has at least one number, and the stretch must contain at least one.
Test data
TEST DATA 1: values is [-2, 1, -3, 4, -1, 2, 1, -5, 4]
TEST DATA 2: values is [-3, -1, -4]
Practice focus
Prefix sums, counting, binary search, best stretch and sorting, all written as plain loops. This is where loops turn into named algorithms. You have already written the loops.