Maximum Employees to Be Invited to a Meeting - Problem

A company is organizing a meeting with n employees around a large circular table. Each employee has exactly one favorite person they want to sit next to, and they'll only attend if this condition is met.

Think of this as a graph problem where each employee points to their favorite person, creating a functional graph (each node has exactly one outgoing edge). The challenge is to find the maximum number of employees we can invite while satisfying everyone's seating preferences.

Key insights:

  • Since it's a circular table, we can have cycles of any length
  • We can also have "chains" that lead into cycles
  • For cycles of length > 2, we can only invite the cycle members
  • For cycles of length 2 (mutual favorites), we can extend with chains leading to them

Goal: Return the maximum number of employees that can be invited to the meeting.

Input & Output

example_1.py โ€” Basic 2-cycles
$ Input: [2,2,1,2]
โ€บ Output: 3
๐Ÿ’ก Note: Employee 1 and 2 are mutual favorites (1โ†’2, 2โ†’1). Employee 0 favorites employee 2, so we can seat them as 0โ†’2โ†”1, creating a chain of length 3. Employee 3 also favorites 2, but we can only extend one side of the 2-cycle.
example_2.py โ€” Large cycle
$ Input: [1,2,0]
โ€บ Output: 3
๐Ÿ’ก Note: We have a 3-cycle: 0โ†’1โ†’2โ†’0. Since this is a cycle of length > 2, we can invite all 3 employees and seat them in this exact circular order.
example_3.py โ€” Multiple 2-cycles with chains
$ Input: [3,0,1,4]
โ€บ Output: 4
๐Ÿ’ก Note: We have one 2-cycle: 0โ†”3 (0โ†’3, 3โ†’0). Employees 1 and 2 form a separate 2-cycle: 1โ†’2โ†’1. Wait, that's not right. Let me recalculate: 0โ†’3, 1โ†’0, 2โ†’1, 3โ†’4. This creates: 2โ†’1โ†’0โ†”3โ†’4, but 4 would need to point somewhere. Let me use a simpler example.

Constraints

  • n == favorite.length
  • 2 โ‰ค n โ‰ค 105
  • 0 โ‰ค favorite[i] โ‰ค n - 1
  • favorite[i] != i (no self-favorites)

Visualization

Tap to expand
Functional Graph: Two Valid StructuresOption 1: Multiple 2-Cycles + ChainsABCDEFCan seat: Aโ†’Bโ†”C and Dโ†’Eโ†”FTotal invited: 6 peopleOption 2: Single Large CyclePQRSTUCan seat: Pโ†’Qโ†’Rโ†’Sโ†’Tโ†’Uโ†’PTotal invited: 6 people๐ŸŽฏ Algorithm ChoiceCompare:Sum of all 2-cycles + chainsLargest single cycleReturn maximum!
Understanding the Visualization
1
Identify Graph Type
Each employee points to exactly one favorite - this creates a functional graph
2
Find Cycles
Every connected component must eventually form a cycle
3
Classify Cycles
Large cycles (>2) vs 2-cycles (mutual pairs)
4
Extend 2-Cycles
2-cycles can be extended with incoming chains
5
Choose Maximum
Pick either the largest single cycle or sum of all extended 2-cycles
Key Takeaway
๐ŸŽฏ Key Insight: In a functional graph, we can either invite one complete large cycle OR multiple 2-cycles extended with their incoming chains - never mix both approaches!
Asked in
Google 15 Amazon 8 Meta 12 Microsoft 6
26.2K Views
Medium Frequency
~35 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen