Given the results of group stage matches in a champions league for one group. You need to determine the team on top of the group’s table and the runner up. The group consists of 4 teams. Hence, 6 matches are held (between each pair).
In a single match, the team with more goals wins. When a team wins it gets 3 points, for a draw it gets 1 point and for losing it gets 0. Teams are sorted in descending order according to points, and in case of a tie according to goal difference (number of goals the team scored - the number of goals it conceded). It’s guaranteed that the group’s top team and the runner up are both unique.
This a straight forward implementation problem.The best tool to solve it with is map (in C++) or any equivalent structure in any other programming language. If you don’t know it, you must learn about it. In simple words, it creates a structure like a dictionary, which a field containing information about some word. Both the word and the info can be of any standard type or even (custom object but with some conditions I will not be mentioning).
So here we will have maps like:
map < string , int > points , goals;
The first map tells the number of points associated with each club name. For example points[“barcelona”] indicates the number of points associated with the string “barcelona”.
After we read the input, we fix the number of points associated with each team and the number of goals as well. After this, we can sort the 4 teams with any sorting algorithm (or even simple if cases) using the information in the maps.
In my C++ code, I wrote a custom comparator, that compares between two strings according to the data in the maps and then used it along with std::sort function. Please refer to my code for details.