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