I couldn’t solve it by my algorithm which was calculating the number of possible ways for each player. But it needed calculation for each player which would have made it exceed TL.
In short, how would you explain your solution? I just need a few hints if possible
@thespacedude, you said a few hints, I’ll give you one: for each player to go to the finals it has to be on a set (supposing we have two sets) in which he is the strongest one, for example, for the arrangement {1, 2, 3, 4} we have the sets {1, 2} and {3, 4}. 2 and 4 are the strongest on their own set. If you work around this it’s pretty straight forward to calculate the solution for each number but if you still need help refer to @bugkiller’s answer or I can explain the full solution (I don’t see the editorials yet).
which is nothing but (n/2)!x(n/2)!x2x((player_strength-1)C(n/2-1))(simple choosing which players can come in current player’s half and then permuting them)
you can precompute (n/2)!x(n/2)!x2
and further
4C3 can be written as 4x3x2/(3!)
5C3 can be written as 5x4x3/(3!)
6C3 can be written as 6x5x4/(3!)
7C3 can be written as 7x6x5/(3!)
you can observe that at ever step one multiplication and one division is there
Let’s divide the array into two halves, H1 and H2 . |H1| ,|H2| are always even as N is a power of 2 ( 4,8… Base case should be treated separately N=2. ) . For simplicity sake, Let’s assume player with strength N is on H1 , Then the player who reaches the final is the player in the other half of the array and with the greatest strength in that other half. Clearly, no one with strength till (N-1)/2 can reach the final .
Ans is sum of 2 * (n/2)! * (n/2)! * (i C n/2-1 ) …… That is , choosing a half side for player with strength N * permuting the two halves with n/2 elements each * selecting n/2-1 smaller elements in the other half as player with strength i+1 is the player with maximum strength in the other half.
ur solution is correct, but u need not hardcode output as 0 for i < n/2. ur formula (the one involving nCr) will produce the right answer for all i. all u need to do is handle nCr = 0 if r>n
@prav19
If we devide the group of players into 2 halves of n/2 size each then there will one player from each half in the final.
For any player, place him in any half and then decide which players can be in his half so that he wins which is
((player_strength-1)C(n/2-1))(selecting the players which have lower strength than the current one)
once you have selected the players in each half you can arrange them in their halves which will be (n/2)! for each half.
as they are independent it will become (n/2)!x(n/2)! plus you can also interchange these to halves which is 2!
hence (n/2)!x(n/2)!x2!