Football Game – Team Winner Problem

In this CPP tutorial, we are going to discuss a football game problem similar type of question has been recently asked in a short contest on Codechef.

Football Game Team winner problem in C++

In a football league, there are  4 teams in a group. Each team plays match with all other teams in its group and one match with other teams in other groups. So, in total, all teams of a group will play 12 matches.

You are provided with team name number of goal it scores in a match his opponent team score and its name like that you are provided with 12 matches details. You have to print the winner of the qualifying league.

Winner of the match is decided by the team which wins match will score 3 points so, the team with most score will be the winner of qualifying league, if there is a tie between two teams in a match then both team will get 1 point and if two teams have the same points in total then team with more total goal difference will be winner. Where goal difference for a team is (sum of number of goals made by it – sum of number of goals it received ).

Algorithm: Football game problem

  1. Take two unordered maps goal_dif and win of (string,int) where goal_dif will store the goal difference of a team and win will score the points scored by each team.
  2. Clear both the map in initial using map.clear().
  3. Now, store the value in map for all 12 matches.
  4. If in a match goal made by team is more than goal score by it then add value 3 in win map of that team otherwise if there is a tie add value 1 in map for both teams.
  5. Also, store the value of goal difference for each team in goal_dif map.
  6. Now, iterate through the map and check for team with highest points if there is two team with highest points then then check for team with more goal difference.

What is map in C++?

Maps are associative container which stores the elements in a mapped fashion means it stores elements in (key,mapped value) pairs, where no two mapped values have same key.

Some basic functions of map are:-

1.) map.clear() :- clears all values from the map.

2.) map.begin():– returns an iterator to the first value in map.

3.) map.end():- returns an element to theoretical last element that follows the last element in map.

4.) map.size():- returns the number of elements in the map.

5.) map.erase(const str):- erase the value str from the map.

6.) map.insert(pair(key,value)):- insert a new element in map with (key,value) pair.


C++ code implementation of football team winner problem


#define Fast  std::ios::sync_with_stdio(false);  cin.tie(NULL);

#define pb  push_back

#define mp  make_pair

#define fi  first

#define se  second

#define all(x) x.begin(),x.end()

#define p pair<ll,ll>

#define pii pair<int,int>

#define mod (int)(1e9+7)

#define PI  (double)(3.14159265358979323846264338327950)

using namespace std;

typedef long long ll;

int a[100005];

void solve()
    int home_goals, away_goals, highest_wins, i;
     string home_team, away_team, vs, highest_team;
         unordered_map<string,int>::iterator itr;
                 else if(home_goals<away_goals)

          if(highest_wins==-1 || (highest_wins<itr->second) || ((highest_wins==itr->second) && (goal_dif[highest_team]          <goal_dif[itr->first])))

 int main()


manutd 8 vs. 2 arsenal

lyon 1 vs. 2 manutd

fcbarca 0 vs. 0 lyon

fcbarca 5 vs. 1 arsenal

manutd 3 vs. 1 fcbarca

arsenal 6 vs. 0 lyon

arsenal 0 vs. 0 manutd

manutd 4 vs. 2 lyon

arsenal 2 vs. 2 fcbarca

lyon 0 vs. 3 fcbarca

lyon 1 vs. 0 arsenal

fcbarca 0 vs. 1 manutd



You may also learn,

Leave a Reply

Your email address will not be published. Required fields are marked *