Related Articles
Print the names of the teams in increasing order of their rankings
• Difficulty Level : Hard
• Last Updated : 08 Apr, 2021

Given a 2D array of strings arr[], representing the scores of M football matches played in a tournament involving N teams, the task is to print the names of the teams in ascending order of their ranks.

• The rules of a match are as follows:
• Each team plays 2 matches.
• The winning team gets 2 points and the losing team gets 0.
• In the case of a tie, both teams share a point each.
• If GD, GA, and GF stand for Goal Difference, Goals Against, and Goals For respectively. The rank of a team is decided in the following priority order:
• Points > GD > GF > Lexicographical order of names.

Examples:

Input: arr[][] = { { “Spain”, “England”, “3″, “0″ }, { “England”, “France”, “1″, “1″ }, { “Spain”, “France”, “0”, “2” } }, N = 3, M = 3
Output: France Spain England
Explanation: Points Table after 3 matches apiece:

Input:  arr[][] = { { “Spain”, “England”, “3″, “0″ }, { “England”, “France”, “1″, “1″ }, { “Spain”, “Spain”, “0”, “2” } }, N = 3, M = 3
Output: Invalid Input

Approach: The problem can be solved using sorting a dictionary by multiple attributes.
Follow the steps below to solve the problem:

• Traverse the list arr[][] and print “Invalid” if arr[i] is equal to arr[i] and break.
• Initialize a dictionary say table to store the GA, GF, and GD for a particular teams.
• Traverse the list arr[][] and perform the following operations:
• Increment GF for arr[i], table[arr[i]] by arr[i] and GA for the arr[i], table[arr[i]] by arr[i].
• Increment GF for arr[i], table[arr[i]] by arr[i] and GA for the arr[i], table[arr[i]] by arr[i].
• Update GD for arr[i], table[arr[i]] by table[arr[i] – table[arr[i].
• Update GD for arr[i], table[arr[i]] by table[arr[i] – table[arr[i].
• If arr[i] == arr[i], then increment table[arr[i] and table[arr[i]] both by 1.
• If arr[i] > arr[i], then increment table[arr[i] by 2.
• If arr[i] < arr[i], then increment table[arr[i] by 2.
• Now, sort the dictionary table based on the priority points > GD > GF and names as:
• table = sorted(table.items(), key=lambda r: (-r, -r, -r, r))
• Finally, print the names of the teams in sorted table.

Below is the implementation of the above approach:

## Python3

 `# Python program for the above approach`` ` `# Function to find the ranking of teams``def` `RankTeams(arr):`` ` `    ``# Traverse the list arr``    ``for` `i ``in` `range``(``len``(arr)):``       ` `        ``# arr[i] is equal to arr[i]``        ``if``(arr[i][``0``] ``=``=` `arr[i][``1``]):``            ``print``(``"Invalid"``)``            ``return``           ` `        ``# Convert the goal to integer``        ``arr[i][``2``] ``=` `int``(arr[i][``2``])``        ``arr[i][``3``] ``=` `int``(arr[i][``3``])`` ` `    ``# Stores the list of GD, GA, and GF``    ``table ``=` `{}``     ` `    ``# Traverse the list``    ``for` `i ``in` `range``(``len``(arr)):``       ` `        ``# Store the list of GA, GF ``        ``# and GD of first team``        ``li1 ``=` `[``0``] ``*` `4``         ` `        ``# Store the list of GA, GF ``        ``# and GD of second team``        ``li2 ``=` `[``0``] ``*` `4`` ` `        ``# If arr[i] is in table``        ``if` `arr[i][``0``] ``in` `table:``            ``li1 ``=` `table[arr[i][``0``]]``             ` `        ``# If arr[i] is in table``        ``if` `arr[i][``1``] ``in` `table:``            ``li2 ``=` `table[arr[i][``1``]]`` ` `        ``# Increment GF by arr[i]``        ``li1[``2``] ``+``=` `arr[i][``2``]``         ` `        ``# Increment GA by arr[i]``        ``li1[``3``] ``+``=` `arr[i][``3``]`` ` `        ``# Increment GF by arr[i]``        ``li2[``2``] ``+``=` `arr[i][``3``]``         ` `        ``# Increment GA by arr[i]``        ``li2[``3``] ``+``=` `arr[i][``2``]`` ` `        ``# Update GD``        ``li1[``1``] ``=` `li1[``2``] ``-` `li1[``3``]``        ``li2[``1``] ``=` `li2[``2``] ``-` `li2[``3``]`` ` `        ``# If tie``        ``if``(arr[i][``2``] ``=``=` `arr[i][``3``]):``            ``li1[``0``] ``+``=` `1``            ``li2[``0``] ``+``=` `1`` ` `        ``# If arr[i] wins``        ``elif``(arr[i][``2``] > arr[i][``3``]):``            ``li1[``0``] ``+``=` `2`` ` `        ``# If arr[i] wins``        ``elif``(arr[i][``2``] < arr[i][``3``]):``            ``li2[``0``] ``+``=` `2`` ` `        ``# Update list in table``        ``table[arr[i][``0``]] ``=` `li1``        ``table[arr[i][``1``]] ``=` `li2`` ` `    ``# Traverse the sortd table in the given priority``    ``for` `key, value ``in` `sorted``(table.items(),``                             ``key ``=` `lambda` `r: (``-``r[``1``][``0``],``                                            ``-``r[``1``][``1``],``                                            ``-``r[``1``][``2``],``                                            ``r[``0``])):``        ``# Print the team name``        ``print``(key, end ``=``'\n'``)`` ` ` ` `# Driver Code`` ` `# Input``arr ``=` `[[``'Spain'``, ``'England'``, ``'3'``, ``'0'``], ``       ``[``'England'``, ``'France'``, ``'1'``, ``'1'``],``       ``[``'Spain'``, ``'France'``, ``'0'``, ``'2'``]]`` ` `RankTeams(arr)`
Output:
```France
Spain
England
```

Time Complexity: O(N * log(N))
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up