Open In App

The Celebrity Problem | Set-3

Last Updated : 31 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 in the party. We can only ask questions like “does A know B? “. Find the stranger (celebrity) in the 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
Also given a list know[], where know[i] is represented in form {a, b} means person a knows person b.

Examples:

Input: know[] = {{1, 3}, {2, 3}, {4, 3}}, N = 4
Output: 2
Explanation: Person with id = 3 does not know anyone, but every other person knows him.

Input: know[] = {{1, 3}, {2, 3}, {3, 2}, {4, 3}}, N = 4
Output: -1
Explanation: No celebrity is present in the party.

 

Approach: Create a vector of int of size N and, increment value of Kth person, if someone knows him, and decrement its value when he knows someone. In the end, the person with value N-1 will be the party celebrity.
Below is the implementation of the above problem:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the party celebrity
int findPartyCelebrity(
    int n,
    vector<vector<int> >& know)
{
 
    vector<int> celebrity(n + 1, 0);
    for (int i = 0; i < know.size(); i++) {
        celebrity[know[i][0]]--;
        celebrity[know[i][1]]++;
    }
    int party_celebrity = -1;
    for (int i = 1; i <= n; i++)
        if (celebrity[i] == n - 1)
            party_celebrity = i;
 
    return party_celebrity;
}
 
// Driver Code
int main()
{
    int n = 3;
    vector<vector<int> > know
        = { { 1, 3 }, { 2, 3 }, { 3, 1 } };
    cout << findPartyCelebrity(n, know);
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to find the party celebrity
  static int findPartyCelebrity( int n, int[][] know)
  {
 
    int celebrity[] = new int[n + 1];
    for (int i = 0; i < know.length; i++) {
      celebrity[know[i][0]]--;
      celebrity[know[i][1]]++;
    }
    int party_celebrity = -1;
    for (int i = 1; i <= n; i++)
      if (celebrity[i] == n - 1)
        party_celebrity = i;
 
    return party_celebrity;
  }
 
  // Driver Code
  public static void main (String[] args) {
    int n = 3;
    int[][] know = { { 1, 3 }, { 2, 3 }, { 3, 1 } };
    System.out.println(findPartyCelebrity(n, know));
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3




# Python code for the above approach
 
# Function to find the party celebrity
def findPartyCelebrity(n, know):
 
    celebrity = [0] * (n + 1)
    for i in range(len(know)):
        celebrity[know[i][0]] -= 1
        celebrity[know[i][1]] += 1
 
    party_celebrity = -1;
    for i in range(1, n + 1):
        if (celebrity[i] == n - 1):
            party_celebrity = i;
 
    return party_celebrity;
 
# Driver Code
n = 3;
know = [[1, 3], [2, 3], [3, 1]];
print(findPartyCelebrity(n, know));
 
# This code is contributed by Saurabh Jaiswal


C#




// C# program for the above approach
using System;
class GFG {
 
  // Function to find the party celebrity
  static int findPartyCelebrity( int n, int[,] know)
  {
 
    int []celebrity = new int[n + 1];
    for (int i = 0; i < know.GetLength(0); i++) {
      celebrity[know[i, 0]]--;
      celebrity[know[i, 1]]++;
    }
    int party_celebrity = -1;
    for (int i = 1; i <= n; i++)
      if (celebrity[i] == n - 1)
        party_celebrity = i;
 
    return party_celebrity;
  }
 
  // Driver Code
  public static void Main () {
    int n = 3;
    int[,] know = { { 1, 3 }, { 2, 3 }, { 3, 1 } };
    Console.WriteLine(findPartyCelebrity(n, know));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
       // JavaScript code for the above approach
 
       // Function to find the party celebrity
       function findPartyCelebrity(
           n,
           know) {
 
           let celebrity = new Array(n + 1).fill(0);
           for (let i = 0; i < know.length; i++) {
               celebrity[know[i][0]]--;
               celebrity[know[i][1]]++;
           }
           let party_celebrity = -1;
           for (let i = 1; i <= n; i++)
               if (celebrity[i] == n - 1)
                   party_celebrity = i;
 
           return party_celebrity;
       }
 
       // Driver Code
 
       let n = 3;
       let know
           = [[1, 3], [2, 3], [3, 1]];
       document.write(findPartyCelebrity(n, know));
 
        // This code is contributed by Potta Lokesh
   </script>


 
 

Output

-1

Time Complexity: O(N)
Auxiliary Space: O(N)

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads