💥💥Beats 100% on runtime and memory [EXPLAINED]
\n\n\n# Intuition\nSimulate a knockout tournament where players face off in pairs, and two special players must be tracked to find out when they compete against
![image.png](https://assets.leetcode.com/users/images/f21ad1f1-f25a-4f79-9182-fd03ae548913_1729331323.2445388.png)\n\n\n# Intuition\nSimulate a knockout tournament where players face off in pairs, and two special players must be tracked to find out when they compete against each other. The goal is to identify the earliest and latest rounds where these two players can meet based on their winning potential.\n\n# Approach\nUse a recursive function to simulate rounds of the tournament, tracking remaining players, and determine matchups until the two special players face each other, while leveraging bit manipulation to manage player states and ensuring that visited states are stored to avoid unnecessary calculations.\n\n# Complexity\n- Time complexity:\nO(n log n) because each round eliminates half of the players, and we process each player multiple times.\n\n- Space complexity:\nO(n) for storing the visited states and the players array.\n# Code\n```typescript []\n/**\n * @param {number} n - Total number of players\n * @param {number} firstPlayer - First important player\n * @param {number} secondPlayer - Second important player\n * @return {number[]} - Array containing the earliest and latest round where both players meet\n */\nfunction earliestAndLatest(n: number, firstPlayer: number, secondPlayer: number): number[] {\n  let earliest: number = n; // Initialize the earliest round to the maximum possible\n  let latest: number = 0;   // Initialize the latest round to 0\n\n  const visited: Set<number> = new Set(); // Track visited game states to avoid repetition\n\n  // Recursive function to simulate rounds\n  const findRounds = (remain: number, players: number[], currentIndex: number, round: number): void => {\n    // If this state has already been visited, return early\n    if (visited.has(remain)) return;\n\n    const player = players[currentIndex];                    // Current player in the matchup\n    const opponent = players[players.length - currentIndex];  // Opponent from the other end\n\n    // If firstPlayer and secondPlayer face each other, update earliest and latest round numbers\n    if ((player === firstPlayer && opponent === secondPlayer) ||\n        (player === secondPlayer && opponent === firstPlayer)) {\n      earliest = Math.min(earliest, round);\n      latest = Math.max(latest, round);\n      return;\n    }\n\n    // If all matchups are completed for this round, move to the next round\n    if (opponent <= player) {\n      const nextPlayers = players.filter(p => remain & (1 << p)); // Filter advancing players\n      findRounds(remain, nextPlayers, 1, round + 1); // Recurse for the next round\n      return;\n    }\n\n    // Simulate both outcomes where player or opponent wins\n    const remainIfPlayerWins = remain ^ (1 << opponent);  // Player wins, opponent eliminated\n    const remainIfOpponentWins = remain ^ (1 << player);  // Opponent wins, player eliminated\n    const nextIndex = currentIndex + 1;\n\n    // Recursion based on which player is important\n    if (player === firstPlayer || player === secondPlayer) {\n      findRounds(remainIfPlayerWins, players, nextIndex, round);\n      return;\n    }\n    if (opponent === firstPlayer || opponent === secondPlayer) {\n      findRounds(remainIfOpponentWins, players, nextIndex, round);\n      return;\n    }\n\n    // Recurse for both outcomes\n    findRounds(remainIfPlayerWins, players, nextIndex, round);\n    findRounds(remainIfOpponentWins, players, nextIndex, round);\n\n    // Mark the current state as visited\n    visited.add(remain);\n  };\n\n  // Create a list of players (1-indexed)\n  const players: number[] = Array.from({ length: n + 1 }, (_, i) => i);\n\n  // Bitmask to represent all players remaining\n  const ALL_PLAYERS_REMAIN = (1 << (n + 1)) - 1;\n\n  // Start the search from the first round\n  findRounds(ALL_PLAYERS_REMAIN, players, 1, 1);\n\n  // Return the earliest and latest round where the two players meet\n  return [earliest, latest];\n}\n\n```
['TypeScript']