Maximum Employees to Be Invited to a Meeting

A company is organizing a meeting and has a list of n employees, waiting to be invited. They have arranged for a large circular table, capable of seating any number of employees.

The employees are numbered from 0 to n - 1. Each employee has a favorite person and they will attend the meeting only if they can sit next to their favorite person at the table. The favorite person of an employee is not themself.

Given a 0-indexed integer array favorite, where favorite[i] denotes the favorite person of the i^th employee, return the maximum number of employees that can be invited to the meeting.

Example 1
Inputfavorite = [2,2,1,2]
Output3
Employees 0, 1, and 2 can be invited and seated so everyone sits next to their favorite, but employee 2 cannot sit beside employees 0, 1, and 3 simultaneously.
Example 2
Inputfavorite = [1,2,0]
Output3
Inviting every employee allows employees 0, 1, and 2 to sit in a circle so each sits next to their favorite.

Constraints

  • n == favorite.length
  • 2 <= n <= 10^5
  • 0 <= favorite[i] <= n - 1
  • favorite[i] != i

Asked at 5 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