{"id":22513856,"url":"https://github.com/emahtab/find-the-celebrity","last_synced_at":"2026-01-06T19:08:39.343Z","repository":{"id":79525490,"uuid":"229938431","full_name":"eMahtab/find-the-celebrity","owner":"eMahtab","description":null,"archived":false,"fork":false,"pushed_at":"2019-12-24T12:42:07.000Z","size":5,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-02T03:25:41.801Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-12-24T12:31:15.000Z","updated_at":"2019-12-24T12:42:24.000Z","dependencies_parsed_at":"2023-05-10T17:15:43.407Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/find-the-celebrity","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Ffind-the-celebrity","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Ffind-the-celebrity/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Ffind-the-celebrity/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Ffind-the-celebrity/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/find-the-celebrity/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245952116,"owners_count":20699428,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-07T03:14:44.068Z","updated_at":"2026-01-06T19:08:39.295Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Find the celebrity\n\nSuppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist one celebrity. The definition of a celebrity is that all the other n - 1 people know him/her but he/she does not know any of them.\n\nNow you want to find out who the celebrity is or verify that there is not one. The only thing you are allowed to do is to ask questions like: \"Hi, A. Do you know B?\" to get information of whether A knows B. You need to find out the celebrity (or verify there is not one) by asking as few questions as possible (in the asymptotic sense).\n\nYou are given a helper function bool knows(a, b) which tells you whether A knows B. \n\nImplement a function int findCelebrity(n), your function should minimize the number of calls to knows.\n\n```\nExample 1\n\nInput:\n2 // next n * (n - 1) lines \n0 knows 1\n1 does not know 0\nOutput: 1\nExplanation:\nEveryone knows 1,and 1 knows no one.\n\nExample 2\n\nInput:\n3 // next n * (n - 1) lines \n0 does not know 1\n0 does not know 2\n1 knows 0\n1 does not know 2\n2 knows 0\n2 knows 1\nOutput: 0\nExplanation:\nEveryone knows 0,and 0 knows no one.\n0 does not know 1,and 1 knows 0.\n2 knows everyone,but 1 does not know 2.\n\nNotice\nThere will be exactly one celebrity if he/she is in the party. \nReturn the celebrity's label if there is a celebrity in the party. If there is no celebrity, return -1.\n\n```\n\n### Implementation \n\n```java\npublic int findCelebrity(int n) {\n    int probableCelebrity = 0;\n    // We find a 'i' who is known by everyone, but doesn't know anyone.\n    for (int i = 1; i \u003c n; i++) {\n\t if (knows(probableCelebrity, i))\n\t\tprobableCelebrity = i;\n    }\n     \n     /* To make sure the value we found out is actually the celebrity, we\n\tcheck if probableCelebrity knows none and everyone knows probableCelebrity.\n      */\n    \n    for (int i = 0; i \u003c n; i++) {\n\t  if (i != probableCelebrity \u0026\u0026 (knows(probableCelebrity, i) || !knows(i, probableCelebrity)))\n\t\treturn -1;\n    }\n\n    return probableCelebrity;\n}\n```\nAbove implementation have runtime complexity of O(n) and space complexity of O(1)\n\n```\nRuntime Complexity = O(n)\nSpace Complexity   = O(1)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Ffind-the-celebrity","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Ffind-the-celebrity","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Ffind-the-celebrity/lists"}