Maximum Employees to Be Invited to a Meeting

You are tasked with organizing a corporate event for n employees. The venue features a massive round table that can accommodate as many people as needed.

The employees are indexed from 0 to n - 1. Each employee has exactly one preferred colleague, represented by a 0-indexed integer array favorite, where favorite[i] is the preferred colleague of employee i.

An employee will only agree to join the event if they are seated immediately adjacent to their preferred colleague at the table. Note that an employee's preferred colleague is never themselves (favorite[i] != i).

Determine the maximum total number of employees you can successfully seat around the table under these strict conditions.

Example 1:

Input: favorite = [2, 2, 1, 2]

Output: 3

Explanation:

Employee 1's favorite is 2, and Employee 2's favorite is 1. They form a mutually favorite pair. Employee 0's favorite is 2. We can seat them at a round table in the order 0 -> 2 -> 1. Employee 2 sits next to 1 and 0. Everyone is happy. The max invited is 3.

Example 2:

Input: favorite = [1, 2, 0]

Output: 3

Explanation:

0 favorites 1, 1 favorites 2, and 2 favorites 0. This forms a closed cycle of 3 people. All 3 can sit around the table.

Now Your Turn!

Pick the correct output for the given input

Input: favorite = [1, 0, 3, 2, 5, 6, 7, 4]

(What is the maximum number of employees that can be invited?)

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • n == favorite.length
  • 2 <= n <= 105
  • 0 <= favorite[i] <= n-1
  • favorite[i] != i

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int maximumInvitations(vector<int>& favorite) {
}
};
Test Case

Input:

Favorite