Skip to content
Veltoryx Technologies
FreeProblem 119● EasyLeetCode #997 ↗4 min readUpdated 30 October 2026

Find the Town Judge

Identify the town judge who trusts nobody and is trusted by everyone else using in-degree and out-degree counting.

01The problem

In a town, there are n people labeled from 1 to n. There is a rumor that one of these people is secretly the town judge.

If the town judge exists, then:

  1. The town judge trusts nobody.
  2. Everybody (except for the town judge) trusts the town judge.
  3. There is exactly one person that satisfies properties 1 and 2.

You are given an array trust where trust[i] = [a_i, b_i] representing that the person labeled a_i trusts the person labeled b_i.

Return the label of the town judge if the town judge exists and can be identified, or return -1 otherwise.

InputOutputWhy
n = 3, trust = [[1, 3], [2, 3]]3Person 3 is trusted by 1 and 2, and trusts nobody.
n = 3, trust = [[1, 3], [2, 3], [3, 1]]-1Person 3 trusts 1, so 3 cannot be judge.

Constraints

  • 1 <= n <= 1000
  • 0 <= trust.length <= 10^4
  • trust[i].length == 2
  • All the pairs of trust are unique.
  • a_i != b_i
  • 1 <= a_i, b_i <= n

02How to spot it

  • "Trusts nobody + trusted by all": In graph terms:
    • Out-degree must be 0 (trusts nobody).
    • In-degree must be n - 1 (trusted by everyone else).
  • Single score array: Maintain score[p] = inDegree[p] - outDegree[p]. The judge must have score[p] == n - 1!

03Pseudocode

text
score = array of n + 1 zeros
for each [a, b]:
  score[a] -= 1 ; score[b] += 1
return p with score[p] == n - 1
(or -1)

04Watch it work

Visualizer · step through the algorithmn = 3, trust = [[1,3],[2,3]]

trust score (1..n)

00
01
02

target score for judge

2

Find town judge among 3 people: score[p] = inDegree - outDegree. Judge must have score == 2.

Pseudocode

  1. score = array of n + 1 zeros
  2. for each [a, b]:
  3. score[a] -= 1 ; score[b] += 1
  4. return p with score[p] == n - 1
  5. (or -1)

Variables

n
3
trust
1,3,2,3
step 1 / 4

05Step-by-step dry run

We trace n = 3, trust = [[1, 3], [2, 3]]:

PersonInitial scoreAfter [1, 3]After [2, 3]Final scoreJudge? (score == 2)
10-1-1-1No
200-1-1No
30+1+2+2YES! (score == 3 - 1)

Output: 3.

06Java solution

java
public class Solution {
    public static int findJudge(int n, int[][] trust) {
        int[] score = new int[n + 1];

        for (int[] t : trust) {
            score[t[0]]--; // out-degree
            score[t[1]]++; // in-degree
        }

        for (int p = 1; p <= n; p++) {
            if (score[p] == n - 1) {
                return p;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[][] trust = {{1, 3}, {2, 3}};
        System.out.println(findJudge(3, trust));
    }
}
Expected output
3

07Why it works

The judge has n - 1 incoming edges and 0 outgoing edges, so inDegree - outDegree = (n - 1) - 0 = n - 1. Any non-judge either trusts someone (reducing their score by at least 1) or is not trusted by everyone (keeping their incoming count below n - 1), making a score of n - 1 uniquely achievable by the judge.

08Time and space

  • Time complexity: O(E + n) where E = trust.length. One pass over trust edges, one pass over n people.
  • Space complexity: O(n) to store the integer score array.

09Edge cases

  • n = 1 and trust = []: Person 1 has score 0 which equals n - 1 = 0. Returns 1.
  • Judge trusts someone: Score drops below n - 1; returns -1.

10Common mistakes

  • Allocating two separate arrays for in-degree and out-degree: While correct, a single array score[i] = in[i] - out[i] is cleaner and halves memory.

11In the interview

  • Opening line: "The town judge is defined by graph degrees: an in-degree of n - 1 and an out-degree of 0. I will maintain a net score array where score[i] = inDegree[i] - outDegree[i]. The judge must have score n - 1."

12Quiz

Why is it sufficient to check only that score[p] == n - 1?

  • (Recommended) Because each person can have at most n - 1 incoming trust edges, so achieving n - 1 requires having 0 outgoing edges and n - 1 incoming edges
  • Because only one person exists in town
  • Because negative scores are disallowed
  • Because town judges always have label n

Answer: Because each person can have at most n - 1 incoming trust edges, so achieving n - 1 requires having 0 outgoing edges and n - 1 incoming edges.

13Solve it yourself

Open LeetCode #997 and submit the degree score solution.