Problem A
This is an adhoc problem. We just need to consider all the possible cases of joining. Note that we can join 2 rectangles only along their common side lengths, if any.
For n = 1, it is trivial, check if it is a square.
For n = 2, only possible way is to join the 2 rectangles.
For n = 3, we have two cases. Either join them all linearly, all 3 in a row kind of fashion. Another way is to join the 2 rectangles to form a greater rectangle and then join third one, perpendicularly.
For all of the above cases, if it is possible to form a square, the side length of square is the largest length / breadth from rectangles.
Problem B
First of all, g(0) = 0.
Since g(g(x)) = -x then using this condition, g(g(g(0))) = -g(0) if we expand 2 outer g's and g(g(g(0))) = g(0) if we expand 2 inner g's. Thus, if g(0) = -g(0) then g(0) = 0.
Now, let g(a) = b then g(b) = g(g(a)) = -a, similarly g(-a) = g(g(b)) = -b and g(-b) = g(g(-a)) = a. Thus we get {a, b, -a, -b}, that is we need to divide the given integers into such groups.
Therefore, if r != -l, then answer is zero, as we need both positive and negative of a number.
Also, if r is odd, then answer is zero, as we need to divide into groups with exactly 2 positive and their negative integers. By using combinatorics, we can get the answer as follows: Number of ways of dividing r integers