Given four numbers a, b, c, d, tell whether it is possible to pair them up such that a:b is equal to c:d. We are allowed to shuffle the order of the numbers.
EXPLANATION:
We can simply try all the possible pairings. A way to model this is to cycle through all the permutations of the four numbers, pair up the first two together and the last two together. Then find the ratio of the first two and the last two; if they are equal, output “Possible”, else “Impossible”. Cycling through permutations can be done through functions like next\_permutation in the C library or simply by recursion. Either way works since we just have 4 numbers.
A more intelligent solution is to sort the four numbers and pair up the first 2 together and the last 2 together and check their ratios. This works because ratios are symmetric.
For checking the ratio equality, we can use an simple property that if a:b = c:d then a*d = b*c. This way, we can avoid dealing with floats.
Please see editorialist’s/setter’s program for implementation details.
“Cycling through permutations can be done through functions like next_permutation in the C library or simply by recursion.”
Or with 4 copypasted cycles + checking if they give a permutation (which can be done with just 4 conditions - three indices must be different and the sum of all 4 must be 0+1+2+3). In this case, it should be the simplest solution.