Given integer N and Okay, denoting the variety of groups taking part in a soccer match the place every workforce performs just one match with one another workforce, the duty is to search out the utmost level distinction between the winner and the runner-up (second place holder) of the match the place the winner fo a match will get Okay factors.
Enter: N = 2, Okay = 4
Rationalization: If there are 2 workforce A and B then both A will win or loss.
If A wins the match it scores 4 factors and workforce B rating is 0.
Therefore most attainable distinction of factors between the successful workforce and the second-placed workforce is 4 .
Enter: N = 3, Okay = 4
Enter: N = 9, Okay = 5
Method: the issue might be solved primarily based on the next mathematical statement:
The distinction might be most when the winner wins all of the matches it performs and all the opposite groups win close to equal matches every. As every time is taking part in towards others solely as soon as so the entire variety of matches is N * (N – 1)/2.
If the winner wins all matches (i.e., (N-1) matches that it performs) the remaining variety of matches are:
(N * (N – 1))/2 – (N – 1) = (N – 1) * (N – 2) / 2.
If every workforce wins close to equal matches, the runner-up will win ceil[ (N – 1)*(N – 2) / (2 * (N – 1)) ] = ceil[ (N – 2)/2 ]
If N is odd: The worth is (N – 2 + 1)/2 = (N – 1)/2
If N is even: The worth is (N – 2)/2.
Matches distinction when N is odd: (N – 1) – (N – 1)/2 = (N – 1)/2. So factors distinction = Okay * ((N – 1)/2).
Matches distinction when N is even: (N – 1) – (N – 2)/2 = N/2. So factors distinction = Okay * (N / 2).
Comply with the steps talked about beneath to implement the concept:
- Examine if N is odd and even.
- If N is odd the required distinction is Okay*((N-1)/2).
- If N is even the required distinction is Okay*(N/2).
Under is the implementation of the above method.
Time Complexity: O(1)
Auxiliary House: O(1)