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.

**Examples:**

Input:M = 3

Output: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

Output:10

**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

N^{2}– N – 2 * M = 0

This is a quadratic equation of type ax^{2}+ bx + c = 0. Here a = 1, b = -1, c = 2 * M. Therefore, applying formula

x = (-b + sqrt(b^{2}– 4ac)) / 2a and x = (-b – sqrt(b^{2}– 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:

## C++

`// C++ implementation of the approach ` `#include <cmath> ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to return the number of teams ` `int` `number_of_teams(` `int` `M) ` `{ ` ` ` `// To store both roots of the equation ` ` ` `int` `N1, N2, sqr; ` ` ` ` ` `// sqrt(b^2 - 4ac) ` ` ` `sqr = ` `sqrt` `(1 + (8 * M)); ` ` ` ` ` `// First root (-b + sqrt(b^2 - 4ac)) / 2a ` ` ` `N1 = (1 + sqr) / 2; ` ` ` ` ` `// Second root (-b - sqrt(b^2 - 4ac)) / 2a ` ` ` `N2 = (1 - sqr) / 2; ` ` ` ` ` `// Return the positive root ` ` ` `if` `(N1 > 0) ` ` ` `return` `N1; ` ` ` `return` `N2; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `M = 45; ` ` ` `cout << number_of_teams(M); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the number of teams ` `static` `int` `number_of_teams(` `int` `M) ` `{ ` ` ` `// To store both roots of the equation ` ` ` `int` `N1, N2, sqr; ` ` ` ` ` `// sqrt(b^2 - 4ac) ` ` ` `sqr = (` `int` `)Math.sqrt(` `1` `+ (` `8` `* M)); ` ` ` ` ` `// First root (-b + sqrt(b^2 - 4ac)) / 2a ` ` ` `N1 = (` `1` `+ sqr) / ` `2` `; ` ` ` ` ` `// Second root (-b - sqrt(b^2 - 4ac)) / 2a ` ` ` `N2 = (` `1` `- sqr) / ` `2` `; ` ` ` ` ` `// Return the positive root ` ` ` `if` `(N1 > ` `0` `) ` ` ` `return` `N1; ` ` ` `return` `N2; ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `M = ` `45` `; ` ` ` `System.out.println( number_of_teams(M)); ` ` ` `} ` `} ` ` ` `// this code is contributed by vt_m.. ` |

*chevron_right*

*filter_none*

## Python3

`# Python implementation of the approach ` `import` `math ` ` ` `# Function to return the number of teams ` `def` `number_of_teams(M): ` ` ` ` ` `# To store both roots of the equation ` ` ` `N1, N2, sqr ` `=` `0` `,` `0` `,` `0` ` ` ` ` `# sqrt(b^2 - 4ac) ` ` ` `sqr ` `=` `math.sqrt(` `1` `+` `(` `8` `*` `M)) ` ` ` ` ` `# First root (-b + sqrt(b^2 - 4ac)) / 2a ` ` ` `N1 ` `=` `(` `1` `+` `sqr) ` `/` `2` ` ` ` ` `# Second root (-b - sqrt(b^2 - 4ac)) / 2a ` ` ` `N2 ` `=` `(` `1` `-` `sqr) ` `/` `2` ` ` ` ` `# Return the positive root ` ` ` `if` `(N1 > ` `0` `): ` ` ` `return` `int` `(N1) ` ` ` `return` `int` `(N2) ` ` ` `# Driver code ` `def` `main(): ` ` ` `M ` `=` `45` ` ` `print` `(number_of_teams(M)) ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `main() ` ` ` `# This code has been contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the number of teams ` ` ` `static` `int` `number_of_teams(` `int` `M) ` ` ` `{ ` ` ` `// To store both roots of the equation ` ` ` `int` `N1, N2, sqr; ` ` ` ` ` `// sqrt(b^2 - 4ac) ` ` ` `sqr = (` `int` `)Math.Sqrt(1 + (8 * M)); ` ` ` ` ` `// First root (-b + sqrt(b^2 - 4ac)) / 2a ` ` ` `N1 = (1 + sqr) / 2; ` ` ` ` ` `// Second root (-b - sqrt(b^2 - 4ac)) / 2a ` ` ` `N2 = (1 - sqr) / 2; ` ` ` ` ` `// Return the positive root ` ` ` `if` `(N1 > 0) ` ` ` `return` `N1; ` ` ` `return` `N2; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `M = 45; ` ` ` `Console.WriteLine( number_of_teams(M)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the number of teams ` `function` `number_of_teams(` `$M` `) ` `{ ` ` ` `// To store both roots of the equation ` ` ` ` ` `// sqrt(b^2 - 4ac) ` ` ` `$sqr` `= sqrt(1 + (8 * ` `$M` `)); ` ` ` ` ` `// First root (-b + sqrt(b^2 - 4ac)) / 2a ` ` ` `$N1` `= (1 + ` `$sqr` `) / 2; ` ` ` ` ` `// Second root (-b - sqrt(b^2 - 4ac)) / 2a ` ` ` `$N2` `= (1 - ` `$sqr` `) / 2; ` ` ` ` ` `// Return the positive root ` ` ` `if` `(` `$N1` `> 0) ` ` ` `return` `$N1` `; ` ` ` `return` `$N2` `; ` `} ` ` ` `// Driver code ` `$M` `= 45; ` `echo` `number_of_teams(` `$M` `); ` ` ` `// This code is contributed ` `// by chandan_jnu ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

10

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximum number of teams that can be formed with given persons
- Maximum number of 3-person teams formed from two groups
- Minimum and Maximum number of pairs in m teams of n people
- Find a string which matches all the patterns in the given array
- Number of matches required to find the winner
- Minimum matches the team needs to win to qualify
- Find smallest number with given number of digits and sum of digits under given constraints
- Check if the given array can be reduced to zeros with the given operation performed given number of times
- Queries to increment array elements in a given range by a given value for a given number of times
- Find minimum value of y for the given x values in Q queries from all the given set of lines
- Find an integer in the given range that satisfies the given conditions
- Find the maximum sum (a+b) for a given input integer N satisfying the given condition
- Find the maximum value of Y for a given X from given set of lines
- Find the XOR of the elements in the given range [L, R] with the value K for a given set of queries
- Find points at a given distance on a line of given slope
- Find the smallest number whose digits multiply to a given number n
- Find the Largest number with given number of digits and sum of digits
- Find maximum number that can be formed using digits of a given number
- Given a number N in decimal base, find number of its digits in any base (base b)
- Find the largest good number in the divisors of given number N

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.