✔️ explained solution | beats 90% in time | 95% in space | C++
We first check whether there are common coordinates between the given (x,y) & elements of the given array. If there is we compare it with the previous Manhattan
We first check whether there are common coordinates between the given (x,y) & elements of the given array. If there is we compare it with the previous **Manhattan distance** & if it is less we update the index to be returned to the current one [remember we are asked for the index of the point with smallest Manhattan distance and not the distance itself].\n\nI\'ve used ternary operators here, which are bascially a concise form of if else statements, it reduces lines of code and makes your work look clean, I recommend you learning it.\n\n```\nclass Solution {\npublic:\n    int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n        int M=INT_MAX, soln=-1;\n        for (int i=0; i<points.size(); i++) {\n            \n            // let\'s see if there are any common coordinates or not\n            if (x==points[i][0] || y==points[i][1]) {\n\n                // let\'s store the Manhattan distance to variable t\n                int t= abs(y-points[i][1]) + abs(x-points[i][0]);\n\n                // M stores the previous Manhattan distance value. If the current distance is less we update the index\n                // even if the distance is equal, we don\'t update the index as we\'re asked for the lowest index\n                soln=M>t ? i:soln;\n\n                // we also update the Manhattan distance, for future calculations\n                M = M<t ? M:t;\n            }\n        }\n\n        // we return the index of the point with least Manhattan distance\n        return soln;\n    }\n};\n```\n\n*Hey! If this post clearified your understanding on how to approach the question, please upvote this solution \uD83D\uDCA1*
['Array', 'C++']