Java | Simple | Explained
T/S: O(n)/O(1), where n = size(points)\n\npublic int nearestValidPoint(int x, int y, int[][] points) {\n\tvar nearest = -1;\n\n\tfor (int i = 0, minDistance = I
**T/S:** O(n)/O(1), where n = size(points)\n```\npublic int nearestValidPoint(int x, int y, int[][] points) {\n\tvar nearest = -1;\n\n\tfor (int i = 0, minDistance = Integer.MAX_VALUE; i < points.length; i++)\n\t\tif (x == points[i][0] || y == points[i][1]) { // check valid point\n\t\t\tvar distance = Math.abs(x - points[i][0]) + Math.abs(y - points[i][1]); // find Manhattan distance\n\t\t\t\n\t\t\tif (minDistance > distance) { \n\t\t\t\tminDistance = distance; // maintain the nearest valid point\n\t\t\t\tnearest = i;\n\t\t\t}\n\t\t}\n\treturn nearest;\n}\n```\n***Please upvote if this helps***
['Java']