Find the Town Judge Leetcode Solution

Problem Statement In this problem, we are given n people labelled from 1 to n. We are also given a 2d array trust[][] shows that trust[i]th people trusts trust[i]th people for each 0 <= i < trust.length. We have to find a person “town judge” who does not trust any …

Course Schedule II – LeetCode

You have to attend n number of courses (from 0 to n-1) where some of the courses have prerequisites. For instance: pair [2, 1] represents to attend course 2 you must have taken course 1. Given an integer n representing the total number of courses and the list of courses …

Find the smallest binary digit multiple of given number

Problem Statement The problem “Find the smallest binary digit multiple of given number” states that you are given a decimal number N. So find the smallest multiple of N that contains only the binary digits ‘0’ and ‘1’. Example 37 111 A detailed explanation can be found below in the …

Minimum Operations to convert X to Y

Problem Statement The problem “Minimum Operations to convert X to Y” states that you are given two numbers X and Y, it is needed to convert X into Y using following operations: Starting number is X. Following operations can be performed on X and on the numbers that are generated …

Check if two nodes are on the same path in a Tree

Problem Statement The problem “Check if two nodes are on the same path in a Tree” states that you are given a n-ary tree (directed acyclic graph) rooted at root node with uni-directional edges between it’s vertices. You are also given a list of queries q. Each query in list …

Distance of nearest cell having 1 in a binary matrix

Problem Statement The problem “Distance of nearest cell having 1 in a binary matrix” states that you are given a binary matrix(containing only 0s and 1s) with at least one 1. Find the distance of the nearest cell having 1 in the binary matrix for all the elements of the …

Transpose Graph

Problem Statement The problem “Transpose graph” states that you are given a graph and you need to find the transpose of the given graph. Transpose: Transpose of a directed graph produces another graph with same edge & node configurations but the direction of all the edges have been reversed. Example …

Count the number of nodes at given level in a tree using BFS

Description The problem “Count the number of nodes at given level in a tree using BFS” states that you are given a Tree (acyclic graph) and a root node, find out number of nodes at L-th level. Acyclic Graph: It is a network of nodes connected through edges which has …

BFS for Disconnected Graph

Problem Statement The problem “BFS for Disconnected Graph” states that you are given a disconnected directed graph, print the BFS traversal of the graph. Example The BFS traversal of the graph above gives: 0 1 2 5 3 4 6 Approach Breadth first Search (BFS) traversal for Disconnected Directed Graph …

Minimum Steps to reach target by a Knight

Description The problem “Minimum Steps to reach target by a Knight” states that you are given a square chess board of N x N dimensions, co-ordinates of the Knight piece, and the target cell. Find out the minimum number of steps taken by the Knight piece to reach the target …