Simple for Beginners Java Approach
Q & A\n##### Q1: Why have you initialized index with -1 and not 0?\n##### \n##### A1: Make the code more general. e.g., If no point has the same x or y coordina
##### Q & A\n##### Q1: Why have you initialized index with -1 and not 0?\n##### \n##### A1: Make the code more general. e.g., If no point has the same x or y coordinate, then we can still detect it by the return value. Otherwise, if the return value is 0, we would NOT know whether the point at index 0 is the solution or not.\n\n```\nclass Solution {\n    public int nearestValidPoint(int x, int y, int[][] points) {\n        int min=Integer.MAX_VALUE, index=-1, i;\n        \n        for ( i=0;i<points.length;i++){\n            if (x==points[i][0] ||  y==points[i][1]){\n                 int d = Math.abs(x - points[i][0]) + Math.abs(y - points[i][1]);\n                if (d<min){\n                    min=d;\n                    index=i;\n                }\n            }\n            \n        }\n        // if ( min== Integer.MAX_VALUE) return -1; --> no longer needed as index is initialized as -1 in the declartion.\n        return  index;\n        \n    }\n}\n```\n\nPlease Upvote if it help :) (it boost my confidence!)
['Java']