Open In App
Related Articles

Number of matches required to find the winner

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that will tell
// no. of matches required
int noOfMatches(int N)
{
    return N - 1;
}
  
// Driver code
int main()
{
    int N = 8;
  
    cout << "Matches played = " << noOfMatches(N);
  
    return 0;
}

                    

Java

// Java implementation of above approach
import java.io.*;
  
class GFG 
{
      
// Function that will tell 
// no. of matches required 
static int noOfMatches(int N) 
    return N - 1
  
// Driver code 
public static void main (String[] args) 
{
    int N = 8
    System.out.println ("Matches played = " +
                          noOfMatches(N)); 
}
}
  
// This code is contributed by jit_t

                    

Python3

# Python 3 implementation of 
# above approach
  
# Function that will tell
# no. of matches required
def noOfMatches(N) :
  
    return N - 1
  
# Driver code
if __name__ == "__main__" :
  
    N = 8
  
    print("Matches played ="
              noOfMatches(N))
  
# This code is contributed
# by ANKITRAI1

                    

C#

//C# implementation of above approach
using System;
  
public class GFG{
      
          
// Function that will tell 
// no. of matches required 
static int noOfMatches(int N) 
    return N - 1; 
  
// Driver code 
      
    static public void Main (){
    int N = 8; 
    Console.WriteLine("Matches played = " +
                        noOfMatches(N)); 
}
}
  
// This code is contributed by ajit

                    

PHP

<?php
// PHP implementation of above approach 
  
// Function that will tell 
// no. of matches required 
function noOfMatches($N
    return ($N - 1); 
  
// Driver code 
$N = 8; 
echo "Matches played = "
         noOfMatches($N); 
  
// This code is contributed by akt_mit
?>

                    

Javascript

<script>
  
// Javascript implementation of above approach
  
// Function that will tell
// no. of matches required
function noOfMatches(N)
{
    return N - 1;
}
  
// Driver code
var N = 8;
  
document.write("Matches played = "
               noOfMatches(N));
                 
// This code is contributed by rutvik_56
  
</script>

                    

Output: 
Matches played = 7

 

Time Complexity: O(1)
Auxiliary Space: O(1)



Last Updated : 13 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads