Skip to content

Instantly share code, notes, and snippets.

@Angelfire
Created December 1, 2022 02:42
Show Gist options
  • Save Angelfire/884f4c9d64fc6b371299a13c9f59bb7f to your computer and use it in GitHub Desktop.
Save Angelfire/884f4c9d64fc6b371299a13c9f59bb7f to your computer and use it in GitHub Desktop.
Three Consecutive Integers
/* Examples
--------
highestConsecutive([491, 846, 835, 202, 863, 336, 372, 992, 39, 211, 561, 282, 900, 270, 428, 267, 333, 363, 568, 680, 649, 692, 953, 159, 290, 31, 832, 302, 179, 587, 340, 286, 90, 442, 447, 641, 920, 219, 136, 966, 255, 647, 885, 753, 415, 982, 205, 779, 468, 752, 882, 814, 332, 702, 92, 15, 876, 903, 807, 234, 73, 998, 927, 80, 275, 353, 544, 342, 716, 215, 374, 504, 799, 784, 842, 66, 991, 738, 456, 455, 435, 474, 83, 168, 171, 352, 142, 318, 13, 446, 546, 828, 232, 851, 317, 747, 595, 101, 403, 21]) -> The Answer is 2586 for subarray [ 876, 903, 807 ]
*/
function highestConsecutive(arr) {
let result = 0
let tempSum = 0
for (let i = 0; i < 3 - 1; i++) {
tempSum += arr[i]
}
for (let i = 3 - 1; i < arr.length; i++) {
tempSum += arr[i]
if (tempSum > result) {
result = tempSum
}
tempSum -= arr[i - 3 + 1]
}
return { result, tempValues }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment