Beats 100%🔥JAVA  | Brute Force
\n\n# Approach\n Describe your approach to solving the problem. \nBrute Force\n\n# Complexity\n- Time complexity: O(n)\n Add your time complexity here, e.g. O(n
\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nBrute Force\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int nearestValidPoint(int x, int y, int[][] points) {\n        int ans=Integer.MAX_VALUE;\n        int min=Integer.MAX_VALUE;\n        int flag=0;\n        for(int i=0;i<points.length;i++){\n            if(points[i][0]==x || points[i][1]==y){\n                if(min>Math.abs(points[i][0]-x)+Math.abs(points[i][1]-y)){\n                    min=Math.abs(points[i][0]-x)+Math.abs(points[i][1]-y);\n                    ans=i;\n                }\n                else if(min==Math.abs(points[i][0]-x)+Math.abs(points[i][1]-y)){\n                    ans=Math.min(ans,i);\n                }\n                flag=1;\n            }\n        }\n        if(flag==0){\n            return -1;\n        }\n        return ans;\n    }\n}\n```
['Java']