Skip to main content

Command Palette

Search for a command to run...

Find the Town Judge

Published
2 min read
Find the Town Judge
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

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<sub>i</sub>, b<sub>i</sub>] representing that the person labeled a<sub>i</sub> trusts the person labeled b<sub>i</sub>. If a trust relationship does not exist in trust array, then such a trust relationship does not exist.

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

LeetCode Problem - 997

class Solution {
    public int findJudge(int n, int[][] trust){
        // Create an array to store the count of trusts for each person.
        int[] count = new int[n+1];

        // Iterate through the trust relationships.
        for (int[] t : trust){
            // Decrement the count for the person making the trust.
            count[t[0]]--;
            // Increment the count for the person being trusted.
            count[t[1]]++;
        }

        // Iterate through the people in the town.
        for (int i = 1; i<=n; i++){
            // If a person's trust count equals 'n-1', they are the judge.
            if (count[i]==n-1) 
                return i;
        }

        // If no judge is found, return -1.
        return -1;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.