Skip to content

Instantly share code, notes, and snippets.

@4skinSkywalker
Created April 17, 2019 11:05
Show Gist options
  • Select an option

  • Save 4skinSkywalker/d7f9f940bf89c0fa9fe17c7a277bb481 to your computer and use it in GitHub Desktop.

Select an option

Save 4skinSkywalker/d7f9f940bf89c0fa9fe17c7a277bb481 to your computer and use it in GitHub Desktop.
function maxKAdjacentSum(array, k) {
if (array.length < k)
return null
let runningSum = 0
for (let i = 0; i < k; i++)
runningSum += array[i]
let highestSum = runningSum
for (let j = 0; j < array.length - k; j++) {
runningSum += array[j + k] - array[j]
highestSum = Math.max(highestSum, runningSum)
}
return highestSum
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment