[ create a new paste ] login | about

Link: http://codepad.org/imUP0yfD    [ raw code | output | fork ]

mohit_at_codepad - Haskell, pasted on Mar 21:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
-- Written by Mohit Jain
-- Print the maximum sum of a sub-list (contigous elements) in given list
-- Input may contain one or negative number
-- Input is empty or contains at least one positive number
main = printMaxSumOfSublist [4, -3, -2, 2, 3, -1, 3, -12, 5, -9, 0, -1]
printMaxSumOfSublist = putStr . show . findMaxSum

findMaxSum = optSumSubList 0 0

optSumSubList bestSumSoFar _ [] = bestSumSoFar
optSumSubList bestSumSoFar sumEndingHere x = optSumSubList newBest newSum remX where
                                             remX = tail x
                                             newBest = max bestSumSoFar newSum
                                             newSum = max 0 sumWithEle
                                             sumWithEle = sumEndingHere + head x


Output:
1
7


Create a new paste based on this one


Comments: