C#
One pass O(N)\n\npublic class Solution\n{\n    public int NearestValidPoint(int x, int y, int[][] points)\n    {\n        int minIdx = -1;\n        int minDista
One pass O(N)\n```\npublic class Solution\n{\n    public int NearestValidPoint(int x, int y, int[][] points)\n    {\n        int minIdx = -1;\n        int minDistance = int.MaxValue;\n        for (int i = 0; i < points.Length; ++i)\n        {\n            var point = points[i];\n            int distance;\n            // No need to calculate the sum, since one of the coordinates is the same and the difference is zero\n            if (point[0] == x)\n                distance = Math.Abs(point[1] - y);\n            else if (point[1] == y)\n                distance = Math.Abs(point[0] - x);\n            else // point[0] != x && point[1] != y therefore not valid\n                continue;\n            if (distance < minDistance)\n            {\n                minDistance = distance;\n                minIdx = i;\n            }\n        }\n        return minIdx;\n    }\n}\n```\n\nAnd using Linq just for the heck of it. Yes, it\'s slower since it involves OrderBy, just as an exercise.\n```\npublic class Solution\n{\n    public int NearestValidPoint(int x, int y, int[][] points)\n    {\n        return points\n            .Select((point, idx) => (Idx: idx, X: point[0], Y: point[1], Distance: Math.Abs(point[0] - x) + Math.Abs(point[1] - y)))\n            .Where(point => point.X == x || point.Y == y)\n            .OrderBy(point => point.Distance)\n            .ThenBy(point => point.Idx)\n\t\t\t.Select(point => point.Idx)\n\t\t\t.DefaultIfEmpty(-1)\n\t\t\t.First();\n    }\n}\n```
[]