Grokking Graph Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Solution: Find the Town Judge
On this page

Problem Statement

You are given a town with n people, labeled from 1 to n. Some people trust others. You need to find out if there is a "town judge". The town judge is someone who:

  1. Everyone else trusts.
  2. The town judge trusts nobody.

Given n and an array trust, where trust[i] = [a<sub>i</sub>, b<sub>i</sub>] means person a<sub>i</sub> trusts person b<sub>i</sub>, return the label of town judge. If there is no town judge, return -1.

Examples

Example 1:

  • Input: n = 4, trust = [[1, 2], [3, 2], [4, 2]]
  • Expected Output: 2

.....

.....

.....

Like the course? Get enrolled and start learning!

On this page