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

0

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

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



Cpp Program Code:

To find the stranger (celebrity) in the party, we can use a two-pass algorithm. In the first pass, we can find a potential celebrity by iterating through all the people and keeping track of the person who is known by everyone else. In the second pass, we can verify if the potential celebrity is indeed a celebrity by checking if they know anyone else.

Here's the C++ code for finding the celebrity in the minimum number of questions:




In this code, the knows function checks if person a knows person b. The findCelebrity function first finds a potential celebrity by iterating through all the people and keeping track of the person who is known by everyone else. Then, it verifies if the potential celebrity is indeed a celebrity by checking if they know anyone else. If the potential celebrity is a celebrity, their ID is returned. If there is no celebrity in the party, -1 is returned. The main function initializes the party and calls the findCelebrity function to find the celebrity in the party.


Python Program code:

here's the Python code for finding the stranger/celebrity in the minimum number of questions, given the input in the same format as the example provided:


In this code, the knows function checks if person a knows person b. The find_celebrity function uses the two-pointer approach to find a potential celebrity by starting with the leftmost and rightmost persons and checking if the left person knows the right person. If the left person knows the right person, then the left person cannot be a celebrity, and we move the left pointer to the right. Otherwise, the right person cannot be a celebrity, and we move the right pointer to the left. We repeat this until the left and right pointers meet at the same person, which is a potential celebrity. Then, we check if the potential celebrity is indeed a celebrity by checking if they know anyone else and if everyone else knows them. If the potential celebrity is a celebrity, their ID is returned. If there is no celebrity in the party, -1 is returned. The example usage initializes the party and calls the find_celebrity function to find the celebrity in the party.



Post a Comment

0Comments
Post a Comment (0)