# SRM 742 Div II - C++ vs Haskell

SRM 742 was held on November 28th. Since I was participating and I am also currently learning Haskell, I decided to solve all the problems in Haskell as an exercise and then I found it interesting to compare it with C++ solutions.

## BirthdayCandy - Div 2 Easy

Short problem statement: Elisa can pick one of the N bags, each of them holding S amount of candies. Once she picks a bag, she distributes it between her and her K schoolmates in a circular manner (one to each). Once she cannot make a full circle anymore, she takes the rest for herself. Pick a bag so Elisa gets maximum amount of candy.

Solution: This is a simple task and can be solved mostly analytically, we just have to check for each bag how many candies Elisa gets and take the optimal one. To calculate result (how many candies Elisa gets), for a single bag, we just need `div` and `mod` operations to emulate the described distribution algorithm.

### Comparison

Language Lines 100 runs
C++ 8 TBD

Wow! Haskell solution is really amazing here, and this problem is perfect for it. What is beautiful in Haskell solution is that we don’t have to do any initialization and we can even omit one argument of the function.

## SixteenQueens - Div 2 Medium

Short problem statement: We are given a chessboard 50 x 50 and a K queens are already positioned on it in such a way that no queen is attacking any other queens. Given number S, return any S positions on the board where additional S queens can be placed so the mentioned no-attack rule still holds for all the queens on the board.

Solution: What works here is the simplest greedy algorithm - for each queen that is to be placed on the board findy any valid position - place the queen there and repeat the algorithm until all the queens are placed on the board.

Why does this greedy algorithm work in this case? I am not sure. Chessboard size is 50 x 50 cells and there can be at most 16 queens on the board, so I guess that makes the problem “sparse” enough for this to work. But I haven’t yet figured out a formal explanation.