Open In App

Final NBA Match Pairing

Last Updated : 28 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given n teams, return their final contest matches in the form of a string. The n teams are labelled from 1 to n, which represents their initial rank (i.e., Rank 1 is the strongest team and Rank n is the weakest team).

We will use parentheses ‘(‘, and ‘)’ and commas ‘,’ to represent the contest team pairing. We use the parentheses for pairing and the commas for partition. During the pairing process in each round, you always need to follow the strategy of making the rather strong one pair with the rather weak one.

Constraints: n will be in form of 2x where x in in the range [1, 12].

Example:

Input: n = 4
Output: “((1,4),(2,3))”
Explanation:
=> In the first round, we pair the team 1 and 4, the teams 2 and 3 together, as we need to make the strong team and weak team together. And we got (1, 4) , (2, 3).

=> In the second round, the winners of (1, 4) and (2, 3) need to play again to generate the final winner, so you need to add the paratheses outside them. And we got the final answer ((1,4),(2,3)).

Input: n = 8
Output: “(((1,8),(4,5)),((2,7),(3,6)))”
Explanation:
=> First round: (1, 8) , (2, 7) , (3, 6) , (4, 5)
=> Second round: ((1, 8),(4, 5)) , ((2, 7),(3, 6))
=> Third round: (((1, 8),(4, 5)),((2, 7),(3, 6)))
Since the third round will generate the final winner, you need to output the answer (((1,8),(4,5)),((2,7),(3,6))).

Approach:

Create a string vector and write all initial match ups to a separate row along with their parenthesis and commas, using the first unmatched to last unmatched logic.

Then the recursive function is called with the size halved, which basically does the same thing without int to string conversions.

Each round halves the number of teams. Therefore, when size is 1 we reach the base case and return without further modifications.

Steps to solve above problem:

  • Create a merge(), this function will merge matches in the array into a single string:
    • if there is only one match (n == 1), return.
    • Merge first half of the matches with second half of the matches by iterating from 0 to n / 2. Update each match string to include both teams.
    • Recursively call merge() with the updated matches array and n / 2.
  • Create a findContestMatch() that takes the number of teams n:
    • Create an empty array matches[] to store the initial matches.
    • Populate matches[] array with initial matches, where each match is represented as a string containing team numbers.
    • Call merge() with matches vector and n / 2 to merge the matches until only one match is left.
  • Return the final match string.

Below is the implementation of above approach:

C++
#include <bits/stdc++.h>
using namespace std;

// This problem is about finding the final contest matches
// in the NBA playoffs, where the stronger teams play
// against the weaker teams.

// Merges the matches in the given vector into a single
// string.
void merge(vector<string>& matches, int n)
{
    // If there is only one match, return.
    if (n == 1) {
        return;
    }

    // Merge the first half of the matches with the second
    // half of the matches.
    for (int i = 0; i < n / 2; i++) {
        matches[i] = "(" + matches[i] + ","
                     + matches[n - i - 1] + ")";
    }

    // Recursively merge the merged matches.
    merge(matches, n / 2);
}

// Finds the final contest matches for the given number of
// teams.
string findContestMatch(int n)
{
    // Create a vector to store the matches.
    vector<string> matches;

    // Add the initial matches to the vector.
    for (int i = 1; i <= n / 2; i++) {
        matches.push_back("(" + to_string(i) + ","
                          + to_string(n - i + 1) + ")");
    }

    // Merge the matches until there is only one match left.
    merge(matches, n / 2);

    // Return the final match.
    return matches[0];
}

// Drive code with static input.
int main()
{

    int n =32;
    string result = findContestMatch(n);
    cout << result << endl;
    return 0;
}
Java
import java.util.ArrayList;
import java.util.List;

public class ContestMatches {
    // Merges the matches in the given list into a single string.
    public static void merge(List<String> matches, int n) {
        // If there is only one match, return.
        if (n == 1) {
            return;
        }

        // Merge the first half of the matches with the second half of the matches.
        for (int i = 0; i < n / 2; i++) {
            matches.set(i, "(" + matches.get(i) + "," + matches.get(n - i - 1) + ")");
        }

        // Recursively merge the merged matches.
        merge(matches, n / 2);
    }

    // Finds the final contest matches for the given number of teams.
    public static String findContestMatch(int n) {
        // Create a list to store the matches.
        List<String> matches = new ArrayList<>();

        // Add the initial matches to the list.
        for (int i = 1; i <= n / 2; i++) {
            matches.add("(" + i + "," + (n - i + 1) + ")");
        }

        // Merge the matches until there is only one match left.
        merge(matches, n / 2);

        // Return the final match.
        return matches.get(0);
    }

    // Drive code with static input.
    public static void main(String[] args) {
        int n = 32;
        String result = findContestMatch(n);
        System.out.println(result);
    }
}

// This code is contributed by shivamgupta0987654321
Python3
def merge(matches, n):
    # If there is only one match, return.
    if n == 1:
        return

    # Merge the first half of the matches with the second half of the matches.
    for i in range(n // 2):
        matches[i] = f"({matches[i]},{matches[n - i - 1]})"

    # Recursively merge the merged matches.
    merge(matches, n // 2)

def find_contest_match(n):
    # Create a list to store the matches.
    matches = []

    # Add the initial matches to the list.
    for i in range(1, n // 2 + 1):
        matches.append(f"({i},{n - i + 1})")

    # Merge the matches until there is only one match left.
    merge(matches, n // 2)

    # Return the final match.
    return matches[0]

if __name__ == "__main__":
    n = 32
    result = find_contest_match(n)
    print(result)
JavaScript
// This problem is about finding the final contest matches
// in the NBA playoffs, where the stronger teams play
// against the weaker teams.

// Merges the matches in the given array into a single
// string.
function merge(matches, n) {
    // If there is only one match, return.
    if (n === 1) {
        return;
    }

    // Merge the first half of the matches with the second
    // half of the matches.
    for (let i = 0; i < n / 2; i++) {
        matches[i] = `(${matches[i]},${matches[n - i - 1]})`;
    }

    // Recursively merge the merged matches.
    merge(matches, n / 2);
}

// Finds the final contest matches for the given number of
// teams.
function findContestMatch(n) {
    // Create an array to store the matches.
    let matches = [];

    // Add the initial matches to the array.
    for (let i = 1; i <= n / 2; i++) {
        matches.push(`(${i},${n - i + 1})`);
    }

    // Merge the matches until there is only one match left.
    merge(matches, n / 2);

    // Return the final match.
    return matches[0];
}

// Drive code with static input.
let n = 32;
let result = findContestMatch(n);
console.log(result);

Output
((1,4),(2,3))

Time Complexity: O(n log n)
Auxiliary Space: O(n log n)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads