Skip to content
Advertisement

Determining a sublist containing elements with alternant sign for which the sums of absolute value of the elements is maximal

Problem: Let us consider an array with real values (positive and negative) Determine a subarray containing elements with alternating signs (positive followed negative, negative followed by positive) for which the sum of the absolute value of the elements is maximal.

After running the code with this list it doesn’t give me the right output the output is [6, -3, 7, -5, 13, 12, 8, -9] but it should be [6, -3, 7, -5, 13, -9, 8]

JavaScript

Advertisement

Answer

Update: the code is not correct in general, I’m at it …

Update: corrected version see below

This gives the expected result (with a few inline comments):

JavaScript

Output including a few test cases:

JavaScript

Corrected version:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement