✔💯 simple solution with explanation
\nclass Solution {\npublic:\n    int nearestValidPoint(int x, int y, vector<vector<int>>& points) \n    {\n        /*  \n\t\tHere we are just iterating over the
```\nclass Solution {\npublic:\n    int nearestValidPoint(int x, int y, vector<vector<int>>& points) \n    {\n        /*  \n\t\tHere we are just iterating over the given array and if we get any \n\t\tvalid point (i.e. it shares either x-coordinate OR y-coordinate with\n\t\tour obtained points) and if the currDist is more than ManhattanDist\n\t\tthen we update it and over this whole process we maintain a variable \n\t\tfor storing the smallest index.\n        \n         TC -> O(N) , N = number of points in points array\n         SC -> O(1) \n\t\t\n\t\t*/\n        \n        \n        int res = -1;\n        int n = points.size();\n        int dist = INT_MAX;\n        int ManhattanDist;\n        \n        for(int i=0; i<n; i++)\n        {\n            if(points[i][0] == x || points[i][1] == y)\n            {\n                ManhattanDist = abs(x-points[i][0]) + abs(y-points[i][1]);\n                \n                if(ManhattanDist < dist)\n                {\n                    dist = ManhattanDist;\n                    res = i;\n                }\n            }\n        }\n        \n        \n        return res;\n    }\n};\n```
['C', 'C++']