Personal tools

Haskell Quiz/Maximum Sub-Array/Solution Jkramar

From HaskellWiki

Jump to: navigation, search


maxSubArray :: (Num a, Ord a) => [a] -> [a]
maxSubArray xs = drop from$take to xs where
  sumswithpos = zip (scanl (+) 0 xs) [0..]
  diff ((a,ai),(b,bi)) = (a-b,(bi,ai))
  (from,to) = snd$maximum$map diff$zip sumswithpos$scanl1 min sumswithpos