Celebrity Problem: Find the Party Celebrity in O(N) Using Two Pointers
The Celebrity Problem is a classic coding interview question where a celebrity is defined as someone who knows no one but is known by everyone, represented in an N×N matrix. A brute-force approach checks every row and column individually, yielding O(N²) time complexity. A more efficient solution uses a two-pointer elimination strategy: if person A knows person B, A is eliminated; if A does not know B, B is eliminated. After N-1 comparisons, one candidate remains and is verified by checking their full row and column. This optimized approach reduces time complexity to O(N) while maintaining O(1) space.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in