🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 3 Min Lesezeit
0

Celebrity Problem

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht




Problem Statement







A celebrity is a person who:




  • Knows no one.

  • Is known by everyone else.



Given an N × N matrix:




CODE
M[i][j] = 1

→ i knows j






Find the celebrity.



Return:




CODE
Celebrity Index

OR

-1






if no celebrity exists.









Brute Force Intuition



In an interview, you can explain it like this:




Check every person individually. Verify whether they know nobody and everybody else knows them.




This requires checking an entire row and column for every person.






Complexity




  • Time Complexity: O(N²)

  • Space Complexity: O(1)






Brute Force Code






CODE
class Solution {

public int celebrity(int[][] M, int n) {

for (int i = 0; i < n; i++) {

boolean celebrity = true;

for (int j = 0; j < n; j++) {

if (i == j)
continue;

if (M[i][j] == 1 ||
M[j][i] == 0) {

celebrity = false;
break;
}
}

if (celebrity)
return i;
}

return -1;
}
}












Moving Towards the Optimal Approach



Notice an important observation.



Suppose:




CODE
A knows B






Then:




CODE
A

Cannot be Celebrity






Similarly,



If:




CODE
A does NOT know B






Then:




CODE
B

Cannot be Celebrity






So with one comparison,



we eliminate one candidate.









Pattern Recognition



Whenever you see:




  • Eliminate Candidates

  • Pairwise Comparison

  • Find One Possible Answer



Think:



Two Pointers / Elimination









Key Observation



Start with:




CODE
Person 0

Person N-1






Compare:




CODE
Does Left Know Right ?









YES






CODE
Left Cannot Be Celebrity

Move Left++












NO






CODE
Right Cannot Be Celebrity

Move Right--






Eventually,



only one candidate survives.



Now simply verify.









Optimal Approach






Step 1



Keep:




CODE
left = 0

right = n-1












Step 2



If:




CODE
M[left][right] == 1






Move:




CODE
left++






Else:




CODE
right--












Step 3



One candidate remains.



Verify:




  • Entire Row

  • Entire Column









Optimal Java Solution






CODE
class Solution {

public int celebrity(int[][] M, int n) {

int left = 0;
int right = n - 1;

while (left < right) {

if (M[left][right] == 1) {

left++;

} else {

right--;
}
}

int candidate = left;

for (int i = 0; i < n; i++) {

if (i == candidate)
continue;

if (M[candidate][i] == 1 ||
M[i][candidate] == 0) {

return -1;
}
}

return candidate;
}
}












Dry Run






Input






CODE
      0 1 2

0 → [0 1 1]

1 → [0 0 1]

2 → [0 0 0]












Step 1






CODE
Left = 0

Right = 2






Check:




CODE
0 knows 2

YES






Move:




CODE
Left = 1












Step 2



Check:




CODE
1 knows 2

YES






Move:




CODE
Left = 2






Candidate:




CODE
2












Verification



Row:




CODE
0 0 0






Knows nobody ✓



Column:




CODE
1

1

0






Everyone knows 2 ✓



Answer:




CODE
2












Why Two Pointers Work?



Every comparison removes exactly one person from consideration.



After:




CODE
N-1 comparisons






only one possible celebrity remains.



The final verification confirms whether that candidate satisfies the celebrity conditions.









Complexity Analysis




















Metric Complexity
Time Complexity O(N)
Space Complexity O(1)








Interview One-Liner




Eliminate one candidate in every comparison using two pointers, then verify the remaining candidate by checking its row and column.










Pattern Learned






CODE
Pairwise Elimination



One Candidate Left



Verify Candidate









Similar Problems




  • Celebrity Problem

  • Find the Judge (LeetCode)

  • Gas Station

  • Majority Element

  • Boyer-Moore Voting Algorithm









Memory Trick



Think:




CODE
A Knows B ?



Yes



A Cannot Be Celebrity

-------------------

No



B Cannot Be Celebrity









Mental Model






CODE
Compare Two People



Eliminate One



Repeat



One Candidate Left



Verify






Whenever you hear:




"Find the celebrity"




your brain should immediately think:



Candidate Elimination + Verification

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Celebrity Problem

Thematisch verwandte Begriffe: Celebrity, Problem · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...