Given an integer M which is the number of matches played in a tournament and each participating team has played a match with all the other teams. The task is to find how many teams are there in the tournament.
Input: M = 3
If there are 3 teams A, B and C then
A will play a match with B and C
B will play a match with C (B has already played a match with A)
C has already played matches with team A and B
Total matches played are 3
Input: M = 45
Approach: Since each match is played between two teams. So this problem is similar to selecting 2 objects from Given N objects. Therefore the total number of matches will be C(N, 2), where N is the number of participating teams. Therefore,
M = C(N, 2)
M = (N * (N – 1)) / 2
N2 – N – 2 * M = 0
This is a quadratic equation of type ax2 + bx + c = 0. Here a = 1, b = -1, c = 2 * M. Therefore, applying formula
x = (-b + sqrt(b2 – 4ac)) / 2a and x = (-b – sqrt(b2 – 4ac)) / 2a
N = [(-1 * -1) + sqrt((-1 * -1) – (4 * 1 * (-2 * M)))] / 2
N = (1 + sqrt(1 + (8 * M))) / 2 and N = (1 – sqrt(1 + (8 * M))) / 2
After solving the above two equations, we’ll get two values of N. One value will be positive and one negative. Ignore the negative value. Therefore, the number of teams will be the positive root of the above equation.
Below is the implementation of the above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Number of matches required to find the winner
- Maximum number of teams that can be formed with given persons
- Minimum and Maximum number of pairs in m teams of n people
- Maximum number of 3-person teams formed from two groups
- Find a string which matches all the patterns in the given array
- Find the largest number smaller than integer N with maximum number of set bits
- Find minimum number to be divided to make a number a perfect square
- Find the minimum number to be added to N to make it a prime number
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Find smallest possible Number from a given large Number with same count of digits
- Find the total number of composite factor for a given number
- Find count of digits in a number that divide the number
- Find the number of ways to divide number into four parts such that a = c and b = d
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Find the maximum number of composite summands of a number
- Find the largest good number in the divisors of given number N
- Find the smallest number whose digits multiply to a given number n
- Find the number of jumps to reach X in the number line from zero
- Find maximum number that can be formed using digits of a given number
- Find the sequence number of a triangular number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.