🔥 C++ || Beats 99.60% || O(n) time, O(1) space || Easy to understand 🔥
Intuition\n\n\n# Approach\nWe check for each point the coincidence of at least one of the coordinates. If there is a match, then we calculate the distance to th
# Intuition\n![image.png](https://assets.leetcode.com/users/images/8db4cddb-7e13-43df-9434-d55e665d7fe0_1673518543.7430418.png)\n\n# Approach\nWe check for each point **the coincidence of at least one of the coordinates**. If there is a match, then we **calculate the distance to the original point**. If **the distance** turned out **to be less than it was**, then we **update it**, and **write the point index into the result**.\n\n# Complexity\n- Time complexity: $ O(n) $\n- Space complexity: $ O(1) $\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n        int result = -1, min_distance = INT_MAX;\n        for (int i = 0; i < points.size(); i++) {\n            int a = points[i][0], b = points[i][1];\n            if (x == a || y == b) {\n                int distance = abs(x - a) + abs(y - b);\n                if (distance < min_distance) {\n                    min_distance = distance;\n                    result = i;\n                }\n            }\n        }\n        return result;\n    }\n};\n```
['C++']