Skip to content

Instantly share code, notes, and snippets.

@ChrisDobby
Last active January 2, 2023 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisDobby/60f4cbbb3600ab5803618673f05332ec to your computer and use it in GitHub Desktop.
Save ChrisDobby/60f4cbbb3600ab5803618673f05332ec to your computer and use it in GitHub Desktop.
Given an array of integers arr and an integer n, return a subarray of arr of length n where the sum is the largest. Make sure you maintain the order of the original array
const maxSubArray = (arr: number[], len: number) =>
arr.length < len
? []
: arr
.map((_, index, a) => a.slice(index, index + len))
.filter(a => a.length === len)
.sort((arr1, arr2) => Math.max(...arr2) - Math.max(...arr1))[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment