Given **N** teams and the results of round-robin tournament in which no match resulted is draw or tie. The task is to find the order of the teams such that every team has won against its consecutive team.

**Examples:**

Input:N = 4

results[] = {{1, 4}, {4, 3}, {2, 3}, {1, 2}, {2, 1}, {3, 1}, {2, 4}}

Output:2 4 3 1

Explanation:

Team-2 has won against Team-4, in the last match.

Team-3 has lost to Team-4 in the2match and^{nd}

Team-3 won against Team-1 in the second last match.

Therefore, every team has won against the team next to itself.

Input:N = 5

results[] = {{1, 4}, {4, 3}, {2, 3}, {1, 2}, {2, 1},

{3, 1}, {4, 2}, {2, 5}, {5, 3}, {4, 5}, {5, 1}}

Output:4 2 5 3 1

**Approach:** The idea is to maintain a hash-map for every team which stores the teams against which the team has won in the round-robin tournament. This can be done by iterating over the elements of the results and for each winner append it to the list of the hash-map object of that team.

The hashtable for an example is as follows: Key Value Team-1 => [Team-4, Team-2] Team-2 => [Team-3, Team-1, Team-4] Team-3 => [Team-1] Team-4 => [Team-3]

Finally, compute the order of teams using recursively. Below is the illustration of the recursive function:

**Base Case:**When the current length of the teams is 1, return the team.**Recursive Case:**Compute the order of the N – 1 team and after the computation of the teams, Iterate over the order of the teams and find a position where the current team has lost against its previous team. If there is no such position append the team at the last position.

Below is the implementation of the above approach:

## Java

`// Java implementation to find the ` `// order of the teams in a round ` `// robin tournament such that every ` `// team has won against to its next team ` ` ` `import` `java.util.*; ` `import` `java.lang.*; ` ` ` `// A class to represent result ` `// of a match of a tournament. ` `class` `Result { ` ` ` `int` `winner; ` ` ` `int` `loser; ` ` ` ` ` `Result(` `int` `winner, ` `int` `loser) ` ` ` `{ ` ` ` `this` `.winner = winner; ` ` ` `this` `.loser = loser; ` ` ` `} ` `} ` ` ` `public` `class` `TeamOrdering { ` ` ` ` ` `// Function to arrange the teams of ` ` ` `// the round-robin tournament ` ` ` `static` `void` `arrangeTeams(` `int` `[] teams, ` ` ` `Result[] results) ` ` ` `{ ` ` ` `HashMap<Integer, ` ` ` `List<Integer> > ` ` ` `map = ` `new` `HashMap<>(); ` ` ` `int` `winner = ` `0` `; ` ` ` ` ` `// Creating a hashmap of teams ` ` ` `// and the opponents against ` ` ` `// which they have won, using ` ` ` `// the results of the tournament ` ` ` `for` `(` `int` `i = ` `0` `; i < results.length; i++) { ` ` ` `winner = results[i].winner; ` ` ` `if` `(map.containsKey(winner)) { ` ` ` `map.get(winner).add( ` ` ` `results[i].loser); ` ` ` `} ` ` ` `else` `{ ` ` ` `List<Integer> list ` ` ` `= ` `new` `ArrayList<Integer>(); ` ` ` `list.add(results[i].loser); ` ` ` `map.put(winner, list); ` ` ` `} ` ` ` `} ` ` ` `List<Integer> output = ` `new` `ArrayList<>(); ` ` ` ` ` `// Arrange the teams in required order ` ` ` `setInOrder(teams, map, teams.length, output); ` ` ` `Iterator<Integer> it = output.iterator(); ` ` ` ` ` `// Displaying the final output ` ` ` `while` `(it.hasNext()) { ` ` ` `System.out.print(it.next()); ` ` ` `System.out.print(` `" "` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Function to determine ` ` ` `// the order of teams ` ` ` `static` `void` `setInOrder( ` ` ` `int` `[] teams, ` ` ` `HashMap<Integer, List<Integer> > map, ` ` ` `int` `n, List<Integer> output) ` ` ` `{ ` ` ` `// Base Cases ` ` ` `if` `(n < ` `1` `) { ` ` ` `return` `; ` ` ` `} ` ` ` `// If there is only 1 team, ` ` ` `// add it to the output ` ` ` `else` `if` `(n == ` `1` `) { ` ` ` `output.add(teams[n - ` `1` `]); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Recursive call to generate ` ` ` `// output for N-1 teams ` ` ` `setInOrder(teams, map, n - ` `1` `, output); ` ` ` `int` `key = teams[n - ` `1` `]; ` ` ` `int` `i; ` ` ` ` ` `// Finding the position for the ` ` ` `// current team in the output list. ` ` ` `for` `(i = ` `0` `; i < output.size(); i++) { ` ` ` ` ` `// Obtain the team at current ` ` ` `// index in the list ` ` ` `int` `team = output.get(i); ` ` ` ` ` `// Check if it has lost against ` ` ` `// current team ` ` ` `List<Integer> losers = map.get(key); ` ` ` `if` `(losers.indexOf(team) != -` `1` `) ` ` ` `break` `; ` ` ` `} ` ` ` `// Add the current team ` ` ` `// to its final position ` ` ` `output.add(i, key); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `[] teams = { ` `1` `, ` `2` `, ` `3` `, ` `4` `}; ` ` ` `Result[] results = { ` ` ` `new` `Result(` `1` `, ` `4` `), ` ` ` `new` `Result(` `4` `, ` `3` `), ` ` ` `new` `Result(` `2` `, ` `3` `), ` ` ` `new` `Result(` `1` `, ` `2` `), ` ` ` `new` `Result(` `2` `, ` `1` `), ` ` ` `new` `Result(` `3` `, ` `1` `), ` ` ` `new` `Result(` `2` `, ` `4` `) ` ` ` `}; ` ` ` ` ` `// Function Call ` ` ` `arrangeTeams(teams, results); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2 4 3 1

**Performance Analysis:**

**Time Complexity:**O(N^{2})**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.

## Recommended Posts:

- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Check whether every node of binary tree has a value K on itself or its any immediate neighbours
- Maximum sum in an array such that every element has exactly one adjacent element to it
- Minimum nodes to be colored in a Graph such that every node has a colored neighbour
- Color all boxes in line such that every M consecutive boxes are unique
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
- Check if a binary string has two consecutive occurrences of one everywhere
- Check if a number has digits in the given Order
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Count possible binary strings of length N without P consecutive 0s and Q consecutive 1s
- First element of every K sets having consecutive elements with exactly K prime factors less than N
- Level order traversal with direction change after every two levels | Recursive Approach
- Number of pairs such that path between pairs has the two vertices A and B
- Minimum number of Nodes to be removed such that no subtree has more than K nodes
- Flatten Binary Tree in order of Level Order Traversal
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Number of ways an array can be filled with 0s and 1s such that no consecutive elements are 1
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- Minimum K such that every substring of length atleast K contains a character c

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.