Mid/SeniorArrayGreedyMath

Generate Schedule

You are given an integer n representing n teams. You are asked to generate a schedule such that:

  • Each team plays every other team exactly twice: once at home and once away.
  • There is exactly one match per day; the schedule is a list of consecutive days and schedule[i] is the match on day i.
  • No team plays on consecutive days.

Return a 2D integer array schedule, where schedule[i][0] represents the home team and schedule[i][1] represents the away team. If multiple schedules meet the conditions, return any one of them.

If no schedule exists that meets the conditions, return an empty array.

Example 1
Inputn = 3
Output[]
Since all 6 required matches cannot be arranged without at least one team playing on consecutive days, no valid schedule exists.
Example 2
Inputn = 5
Output[[0,1],[2,3],[0,4],[1,2],[3,4],[0,2],[1,3],[2,4],[0,3],[1,4],[2,0],[3,1],[4,0],[2,1],[4,3],[1,0],[3,2],[4,1],[3,0],[4,2]]
This is one valid schedule of all 20 required matches, and no team plays on consecutive days.

Constraints

  • 2 <= n <= 50

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate