Skip to content

Instantly share code, notes, and snippets.

@vaibhavsagar
Created November 29, 2016 23:09
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 vaibhavsagar/0aeb4fa0b10ca65024e4c3b36d749a13 to your computer and use it in GitHub Desktop.
Save vaibhavsagar/0aeb4fa0b10ca65024e4c3b36d749a13 to your computer and use it in GitHub Desktop.
Code Dojo solution
import Data.List (inits, tails)
partitionSum [] n = n
partitionSum ls n = let
pairs = zip (tail $ inits ls) (tail $ tails ls)
pass = filter (\(l,r) -> (sum l) == (sum r)) pairs
maxes = map (\(l,r) -> max (partitionSum l (n+1)) (partitionSum r (n+1))) pass
in if (maxes == []) then n else (maximum maxes)
main = do
noCases <- fmap read getLine :: IO Int
mapM_ (\_ -> do
_ <- getLine
ls' <- fmap words getLine
let ls = map read ls' :: [Int]
print $ partitionSum ls 0) [1..noCases]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment