Maximum Perimeter Triangle

  • + 0 comments

    Haskell

    module Main where
    
    import Data.List (find, sortBy)
    import Data.Maybe (fromMaybe)
    
    overlapping :: Int -> [a] -> [[a]]
    overlapping k xs
        | length xs < k = []
        | otherwise = take k xs : overlapping k (tail xs)
    
    solve :: [Int] -> [Int]
    solve xs = case firstValid of
        Just [x, y, z] -> [z, y, x]
        Nothing -> [-1]
      where
        xs' = sortBy (flip compare) $ filter (> 0) xs
        tripples = overlapping 3 xs'
        firstValid = find (\[x, y, z] -> x < y + z) tripples
    
    main :: IO ()
    main = do
        _ <- getLine
        xs <- map read . words <$> getLine :: IO [Int]
        putStrLn  $ unwords $ map show $ solve xs