Interview question of the issue #281 of rendezvous with cassidoo.
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, and if n
is greater than arr.length
, you can choose what you want to return.
Example:
> maxSubarray([-4,2,-5,1,2,3,6,-5,1], 4)
> [1,2,3,6]
> maxSubarray([1,2,0,5], 2)
> [0,5]
Just pnpm i
to install all dependencies and then pnpm t
to run the tests!