O(n) 🤩 🔥
Complexity

Time complexity:
O(n)


Space complexity:
O(1)

Code
![Screenshot 2025-01-26 at 4.03.46 PM.png](https://assets.leetcode.com/users/images/24462c7a-1dde-4924-89da-d062b54d8205_1737887737.837889.png)

# Complexity
- Time complexity:
$$O(n)$$ 

- Space complexity:
$$O(1)$$

# Code
```cpp []
class Solution {
public:
    int nearestValidPoint(int x, int y, vector<vector<int>>& points) {
        int min = INT_MAX, index = -1;
        for (int i = 0; i < points.size(); i++) {
            int xi = points[i][0];
            int yi = points[i][1];

            // valid points
            if ((x == xi) || (y == yi)) {
                int distance = abs(x - xi) + abs(y - yi);

                // find index
                if (min > distance) {
                    min = distance;
                    index = i;
                }
            }
        }
        return index;
    }
};
```
['Array', 'Math', 'C++']