*In a party of N people, only one person is known to everyone. Such a person may be present in the party, if yes, (s)he doesn’t know anyone in the party. We can only ask questions like “does A know B? “. Find the stranger (celebrity) in minimum number of questions.*

We can describe the problem input as an array of numbers/characters representing persons in the party. We also have a hypothetical function *HaveAcquaintance(A, B)* which returns *true* if A knows B, *false* otherwise. How can we solve the problem.

We measure the complexity in terms of calls made to *HaveAcquaintance().*

**Method 1 (Graph)**

_{C2}pairs. If celebrity is present in the party, we will have one sink node in the graph with outdegree of zero, and indegree of N-1. We can find the sink node in (N) time, but the overall complexity is O(N

^{2}) as we need to construct the graph first.

**Method 2 (Recursion)**

Solve the problem of smaller instance during divide step. On the way back, we find the celebrity (if present) from the smaller instance. During combine stage, check whether the returned celebrity is known to everyone and he doesn’t know anyone. The recurrence of the recursive decomposition is,

T(N) = T(N-1) + O(N)

T(N) = O(N^{2}). You may try writing pseudo code to check your recursion skills.

**Method 3 (Using Stack)**

The graph construction takes O(N

^{2}) time, it is similar to brute force search. In case of recursion, we reduce the problem instance by not more than one, and also combine step may examine M-1 persons (M – instance size).

We have following observation based on elimination technique (Refer *Polya’s How to Solve It* book).

- If A knows B, then A can’t be celebrity. Discard A, and
*B may be celebrity*. - If A doesn’t know B, then B can’t be celebrity. Discard B, and
*A may be celebrity*. - Repeat above two steps till we left with only one person.
- Ensure the remained person is celebrity. (Why do we need this step?)

We can use stack to verity celebrity.

- Push all the celebrities into a stack.
- Pop off top two persons from the stack, discard one person based on return status of
*HaveAcquaintance**(A, B)*. - Push the remained person onto stack.
- Repeat step 2 and 3 until only one person remains in the stack.
- Check the remained person in stack doesn’t have acquaintance with anyone else.

We will discard N elements utmost (Why?). If the celebrity is present in the party, we will call *HaveAcquaintance**()* 3(N-1) times. Here is code using stack.

**C++ Programming:**

**Output :**

Celebrity ID 2

Complexity O(N). Total comparisons 3(N-1). Try the above code for successful MATRIX {{0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}}.

**Note:** You may think that why do we need a new graph as we already have access to input matrix. Note that the matrix MATRIX used to help the hypothetical function *HaveAcquaintance(A, B),* but never accessed via usual notation MATRIX[i, j]. We have access to the input only through the function *HaveAcquiantance(A, B)*. Matrix is just a way to code the solution. We can assume the cost of hypothetical function as O(1).

If still not clear, assume that the function *HaveAcquiantance *accessing information stored in a set of linked lists arranged in levels. List node will have *next* and *nextLevel* pointers. Every level will have N nodes i.e. an N element list, *next* points to next node in the current level list and the *nextLevel* pointer in last node of every list will point to head of next level list. For example the linked list representation of above matrix looks like,

L00->0->1->0 |L10->0->1->0 |L20->0->1->0 |L30->0->1->0

The function *HaveAcquanintance(i, j)* will search in the list for *j-th* node in the *i-th* level. Out goal is to minimize calls to *HaveAcquanintance* function.

**Method 4 (Using two Pointers)**

**C++ Programming:**

**Output :**

Celebrity ID 2