Find Nearest Point that same X or y coordinate | O(n)
I know this Looks so long but bare with me You will understand it thoroughly.\n\n\nclass Solution {\n    \nprivate:\n    int x1=0,y1=0; // To Store give x and y
I know this Looks so long but bare with me You will understand it thoroughly.\n```\n\nclass Solution {\n    \nprivate:\n    int x1=0,y1=0; // To Store give x and y val at start (to decrease manHatDis function memory footprint)\n                   // So that when calling function multiple time we did n\'t need to pass x and y val again and again\npublic:\n    \n    int manHatDis(int x2,int y2) // To calculate Manhattan Distance between (x1,y1) (x2,y2)\n    {\n        return (abs(x1-x2)+abs(y1-y2));\n    }\n    \n    int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n        \n        x1=x;\n        y1=y;// [[1,2],[3,1],[2,4],[2,3],[4,4]]\n        \n        unordered_map<int,vector<int>> manhat; // To store Distance value as key (ManHattanDistance)\n                                               // For each Manhattan Dis we store x2,y2,index for which this Manhattan Dis came\n                                               // Here index is index of point (x2,y2) in Points array\n                                               // eg: for point [2,4] index= 2 || for point [3,1] index= 1\n        int index=0;\n        \n        for(auto point : points)\n        {\n            if(point[0]==x1 || point[1]==y1 ) // if point contain x1 or y1 then only proceed\n            {\n                int currDis= manHatDis(point[0],point[1]); // calculate Man Dis for current point\n            \n                if( manhat.find(currDis)!=manhat.end()) // if this Man Dis already present then \n                {\n                    if(point[0]+point[1] < manhat[currDis][0]+manhat[currDis][1] ) // check if curr point (x+y) < prev store (x+y)\n                    {\n                        manhat[currDis][0]= point[0];  // store x2\n                        manhat[currDis][1]= point[1];  // store y2\n                        manhat[currDis][2]= index;     // update latest index related to (x2,y2)\n                    }\n                }\n                else // if Man Dis not already present then Add fresh\n                {\n                    manhat[currDis].push_back(point[0]); \n                    manhat[currDis].push_back(point[1]);\n                    manhat[currDis].push_back(index);\n                }\n            }\n            \n            index++; \n            \n        }\n        \n        \n        int minDis=INT_MAX; // Intialize with MAX\n        \n        for(auto currDis: manhat) // To find min of all Man Distances Stored so far\n        {\n            if(currDis.first < minDis)\n                minDis= currDis.first;\n        }\n        \n        return minDis==INT_MAX?-1:manhat[minDis][2]; // if not found return -1 else return found Man Dis index\n \n    }\n};\n```
['C', 'C++']