Given a number N which represents the number of players participating in a Badminton match. The task is to determine the number of matches required to determine the Winner. In each Match 1 player is knocked out.
Examples:
Input: N = 4
Output: Matches played = 3
(As after each match only N - 1 players left)
Input: N = 9
Output: Matches played = 8
Approach: Since, after each match, one player is knocked out. So to get the winner, n-1 players should be knocked out and for which n-1 matches to be played.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int noOfMatches( int N)
{
return N - 1;
}
int main()
{
int N = 8;
cout << "Matches played = " << noOfMatches(N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int noOfMatches( int N)
{
return N - 1 ;
}
public static void main (String[] args)
{
int N = 8 ;
System.out.println ( "Matches played = " +
noOfMatches(N));
}
}
|
Python3
def noOfMatches(N) :
return N - 1
if __name__ = = "__main__" :
N = 8
print ( "Matches played =" ,
noOfMatches(N))
|
C#
using System;
public class GFG{
static int noOfMatches( int N)
{
return N - 1;
}
static public void Main (){
int N = 8;
Console.WriteLine( "Matches played = " +
noOfMatches(N));
}
}
|
PHP
<?php
function noOfMatches( $N )
{
return ( $N - 1);
}
$N = 8;
echo "Matches played = " ,
noOfMatches( $N );
?>
|
Javascript
<script>
function noOfMatches(N)
{
return N - 1;
}
var N = 8;
document.write( "Matches played = " +
noOfMatches(N));
</script>
|
Output:
Matches played = 7
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
13 Mar, 2023
Like Article
Save Article