1809. Largest Mutual Circle

Hard7.5 points estimatedBacktrackingBitmaskGraph

A club has n members numbered 0 to n - 1. Each entry friendships[i] = [u, v] says that members u and v are friends; friendship is mutual and no pair is listed twice. A mutual circle is a set of members in which every two members are friends. A single member is a circle of size 1 and a lone friendship pair is a circle of size 2.

Return the size of the largest mutual circle in the club. Only the size is asked for, so ties between different circles of the same size do not matter. A branching search that extends a growing circle through a candidate set, uses a pivot to skip redundant branches and stops as soon as the circle plus its candidates cannot beat the best size found so far runs comfortably within the limits, using 64-bit adjacency masks. Expected time is exponential in n but fast for n up to 40, with O(n) extra space.

Example 1

Input:
n = 5, friendships = [[0,1],[1,2],[0,2],[2,3],[3,4]]
Output:
3
Explanation:

People 0, 1 and 2 are all friends with each other, and no group of four exists, so the answer is 3.

Example 2

Input:
n = 4, friendships = [[0,1],[2,3]]
Output:
2
Explanation:

No three people are mutual friends, so the largest circle is a single friendship pair of size 2.

Example 3

Input:
n = 3, friendships = []
Output:
1
Explanation:

Nobody is friends with anybody, so the best circle is one person.

Constraints

1 ≤ n ≤ 40

friendships[i] = [u, v] with 0 ≤ u, v < n and u ≠ v

No friendship pair is listed twice (in either order)

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 6,000 msC++ 1,500 msJava 3,000 msJavaScript 3,000 msTypeScript 3,000 ms

Expected complexity

Time
O(3^(n/3))
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…