Problem In a town, there are 'n' people labelled from 1 to 'n'. There is a rumor that one of these people is secretly the town judge. If the town judge exists, then: The town judge trusts nobody. Everybody (except for the town judge) trusts the town judge. There is exactly one person that satisfies properties 1 and 2. The given input is an array of pairs trust[i] = [a, b] representing that the person labelled 'a' trusts the person labelled 'b'. If the town judge exists and can be identified, return the label of the town judge. Otherwise, return '-1'. Solution To solve the problem we can use a simple array of size N+1 (we will use N+1 to avoid maintaining numbers as arrays starts with index 0 and we have people numbered from 1). Every index starting one represent a member of the town. We can use following logic to solve the problem. Lets go through the entire list of trust values and do following things For every
Problem Given a list of x, y coordinates, find if all the coordinates are on a straight line. if YES return 'True' otherwise return 'False' Solution Equation for the straight line is y = mx + c where y and x are the coordinates and m and c are integer constants. if all the inputs are on the same straight line they should follow the above formula. We can use first two points to find the value of m and c and check other following points using the formula. if first point is x1, y1 and second point is x2, y2 y1 = m * x1 + c y2 = m * x2 + c m = (y2-y1)/(x2-x1) c = y1-(m*x1) one we have above values for every point we can check if y3 = m*x3+c ... If we put above logic in the java code it will look like this