✅ O(N) Simple solution w/ explanation | C++ short & concise
We can simply iterate over all the points in points array and if a point is valid (has the same x or y co-ordinate as our location), find its manhattan distance
We can simply iterate over all the points in `points` array and if a point is valid (has the same `x` or `y` co-ordinate as our location), find its *manhattan distance* and remember its index if that is the minimum one. Lastly, just return the index.\n```\nint nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n\tint n = points.size(), mn = INT_MAX, ans = -1, manhattan;\n\tfor(int i = 0; i < n; i++)\n\t\tif(points[i][0] == x || points[i][1] == y){\n\t\t\tmanhattan = abs(x - points[i][0]) + abs(y - points[i][1]);\n\t\t\tif(manhattan < mn)\n\t\t\t\tmn = manhattan, ans = i;            \n\t\t}\n\treturn ans;\n}\n```\n**Time Complexity** : **`O(N)`**, where `N` is the numbe of points in the `points` vector.\n**Space Complexity** : **`O(1)`**, since only constant space is used.
[]