Find the Nearest Valid Point to a Given Coordinate
Intuition\n Describe your first thoughts on how to solve this problem. \nWe need to find the index of the point in the given array that is closest to the given 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe need to find the index of the point in the given array that is closest to the given (x, y) coordinate and has either the same x or y coordinate as the given coordinate.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe first create an ArrayList to store all the points that have either the same x or y coordinate as the given coordinate. We then iterate through this ArrayList and calculate the distance between each point and the given coordinate. We keep track of the minimum distance found so far and the corresponding point. Finally, we iterate through the input points array to find the index of the minimum distance point and return it.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n     O(n), where n is the number of points in the input array.\n\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(k), where k is the number of valid points \n\n# Code\n```\nclass Solution {\n    public int nearestValidPoint(int x, int y, int[][] points) {\n         int res = -1;\n        ArrayList<int[]> arr = new ArrayList<>();\n        for (int i = 0; i < points.length; i++) {\n            int xPoint = points[i][0];\n            int yPoint = points[i][1];\n            if(xPoint == x || yPoint == y){\n                arr.add(points[i]);\n                System.out.println("Point added to arr : " + Arrays.toString(points[i]));\n            }\n        }\n        System.out.println("List of valid points : ");\n        arr.forEach(ints -> {\n            System.out.print(Arrays.toString(ints) + " ");\n        });\n        System.out.println();\n        int min = Integer.MAX_VALUE;\n        int[] minPoint = new int[]{0,0};\n        if(!arr.isEmpty()) {\n            for (int i = 0; i < arr.size(); i++) {\n                int current = Math.abs(x - arr.get(i)[0]) + Math.abs(y - arr.get(i)[1]);\n                System.out.println("Current distance : " + current);\n                if (current < min) {\n                    min = current;\n                    minPoint = new int[]{arr.get(i)[0], arr.get(i)[1]};\n                }\n                System.out.println("Min distance : " + min);\n                System.out.println("Min point : " + Arrays.toString(minPoint));\n            }\n\n            for (int i = 0; i < points.length; i++) {\n                if (Arrays.equals(points[i], minPoint)) {\n                    res = i;\n                    System.out.println("Res value added " + i);\n                    return res;\n                }\n            }\n        }\n        return res;\n    }\n}\n```
['Array', 'Java']