- Calculate the total amount.
- Sorting.
- Find the consecutive pair with the smallest difference.
function leastSubsequenceSum(values) { var n = values.length;
<strong> Examples:
>>> leastSubsequenceSum([10, -5, 3, -4, 11, -4, 12, 20]); [2, 3, 1] >>> leastSubsequenceSum([5, 6, -1, -9, -2, 16, 19, 1, -4, 9]); [0, 4, 1] >>> leastSubsequenceSum([3, 16, 8, -10, -1, -8, -3, 10, -2, -4]); [6, 9, 1]
In the first example, [2, 3, 1] means the sum from the index 2 to 3 (inclusive), and you get the absolute sum 1 :
[10, -5, 3, -4, 11, -4, 12, 20] ^^^^^