n possible guests, numbered 0 to n - 1.
Everyone has a best friend: buddy[i] is the best friend of guest i (always someone else). A guest will come only if they get to sit **next to** their best friend.
Return the largest number of guests that can be invited and seated so that every invited guest sits next to their best friend.buddy=[1, 0, 1, 2] → 4
0 and 1 sit together, 2 sits next to 1, and 3 next to 2: going round the table 3, 2, 1, 0, everyone is happy.
buddy=[1, 2, 3, 0, 0] → 4
0, 1, 2, 3 form a ring where everyone sits next to their friend. Guest 4 wants 0, but both of 0's neighbours are taken, so the answer is 4.
buddy=[2, 0, 1, 4, 3] → 3
buddy=[1, 0, 1, 2]
4
Run checks the first 3 tests in your browser. Submit checks all 66 tests on the server, hidden ones included.