Problem Name: 1688. Count of Matches in Tournament
Problem Link: https://leetcode.com/problems/count-of-matches-in-tournament/
Difficulty: Easy
Tag: Math
Language: C# | C++
OJ: LeetCode
Algorithm:
1.Initialize a variable count to 0.
2.Use a while loop that continues until n becomes 1.
✅Inside the loop, add the sum of the current number of teams (n) divided by 2 and the remainder of the division by 2 to count.
✅Update n by dividing it by 2.
3.Return the final value of count.
Code(C#)
public class Solution
{
public int NumberOfMatches(int n)
{
int count = 0;
while(n != 1)
{
count += ((n % 2) + (n / 2));
n /= 2;
}
return count;
}
}
Code(C++)
class Solution {
public:
int numberOfMatches(int n) {
int count = 0;
while(n != 1)
{
count += ((n % 2) + (n / 2));
n /= 2;
}
return count;
}
};