Team Game

Easy
0/40
2 upvotes

Problem statement

You are given an array of characters 'G' of length 'N', where each character represents the outcome of a game. The character 'A' indicates a win for Team A, and the character 'B' indicates a win for Team B.


Your task is to count the number of wins for each team and return the team that has won the most games. If both teams have won the same number of games, return "Tie".


For Example :
Let 'N' = 5, 'G' = ['A', 'B', 'A', 'A', 'B'].
Team A has won 3 games, and Team B has won 2 games.
Therefore, the answer is "A".
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line contains an integer 'N', the number of games.
The second line contains the array of characters 'G' of length 'N', representing the game outcomes.
Output Format :
Return a string representing the team with the most wins ("A" or "B") or "Tie" if both teams have the same number of wins.
Note :
You don’t need to print anything. Just implement the given function.
Constraints :
1 <= 'N' <= 10^5
Each character in 'G' will be either 'A' or 'B'.

Time Limit: 1 sec
Sample Input 1 :
3
A B A
Sample Output 1 :
A
Explanation of sample input 1 :
The array of game outcomes 'G' is ['A', 'B', 'A'], and the number of games 'N' is 3.
By iterating through the array 'G', we find that Team A has won 2 games and Team B has won 1 game.
Since Team A has won more games than Team B, the answer is "A".
Sample Input 2 :
4
B B A A
Sample Output 2 :
Tie
Hint

Traverse the array and keep track of the number of wins for each team.

Approaches (1)
Implementation

Approach:

  • Initialize two variables to store the win counts for Team A and Team B.
  • Iterate through the array of game outcomes.
  • For each game outcome, increment the win count of the corresponding team.
  • After iterating through all the games, compare the win counts of the two teams.
  • Return 'A' if Team A has more wins, 'B' if Team B has more wins, and 'Draw' if they have the same number of wins.

Algorithm:

  • Initialize a variable 'count_A' to 0.
  • Initialize a variable 'count_B' to 0.
  • Iterate using 'i' from 0 to 'N - 1':
    • If (G[i] == 'A'):
      • Increment 'count_A' by 1.
    • Else if (G[i] == 'B'):
      • Increment 'count_B' by 1.
  • If ('count_A' > 'count_B'):
    • Return 'A'.
  • Else if ('count_B' > 'count_A'):
    • Return 'B'.
  • Else:
    • Return 'Draw'.
Time Complexity

O(N), where 'N' is the number of games in the array 'G'.

We iterate through the array 'G' once to count the wins for each team. Thus, the overall time complexity is of the order O(N).

Space Complexity

O(1).

We use a constant amount of extra space to store the win counts for Team A ('count_A') and Team B ('count_B'). Thus, the overall space complexity is of the order O(1).

Code Solution
(100% EXP penalty)
Team Game
Full screen
Console