Skip to main content

Command Palette

Search for a command to run...

Count of Matches in Tournament

Published
2 min read
Count of Matches in Tournament
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

You are given an integer n, the number of teams in a tournament that has strange rules:

  • If the current number of teams is even, each team gets paired with another team. A total of n / 2 matches are played, and n / 2 teams advance to the next round.

  • If the current number of teams is odd, one team randomly advances in the tournament, and the rest gets paired. A total of (n - 1) / 2 matches are played, and (n - 1) / 2 + 1 teams advance to the next round.

Return the number of matches played in the tournament until a winner is decided.

LeetCode Problem - 1688

class Solution {
    public int numberOfMatches(int n) {
        // Initialize a variable to store the total number of matches played
        int answerMatches = 0;

        // Continue looping until only one team remains (i.e., n == 1)
        while(n != 1){
            // Check if the number of teams is even
            if(n % 2 == 0){
                // If even, add half the teams to the match count (since each match eliminates one team)
                answerMatches += n / 2;
                // Update the number of teams after the matches (half the teams move to the next round)
                n = n / 2;
            } else {
                // If odd, add half the teams (rounded down) to the match count
                answerMatches += n / 2;
                // Update the number of teams after the matches (one team moves directly to the next round)
                n = (n - 1) / 2 + 1;
            }
        }
        // Return the total number of matches played
        return answerMatches;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.