C++ Map Solution | Detailed Explanation
Intuition\n Describe your first thoughts on how to solve this problem. \nAs the questions asks about longest distance betweeen 2 students, we need to store the 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nAs the questions asks about longest distance betweeen 2 students, we need to store the indexes where the previous students are currently sitting. So, we can think of `map` data structure.\nWe ierate over the map for each call for `seat` and then find the maximum distance we can achieve.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- If `students == 0` -> place the student at position 0.\n- If `students == 1` -> Check if that 1 student is at position `0`, `n - 1` or in between .\n- Else, iterate over the map and find at what index should the student be placed in order to maximize the closest distance.\n\n# Complexity\n- Time complexity: Seat -> `O(n*log(n))` & Leave -> `O(log(n))`\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: `O(min(seats, 1e4))`\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass ExamRoom {\nprivate:\n    map<int, int> mp;\n    int n;\n\npublic:\n    ExamRoom(int N) {\n        n = N;\n    }\n    \n    int seat() {\n        // 0 students:\n        if(mp.size() == 0) {\n            mp[0] = 1;\n            return 0;\n        }\n\n        // 1 student\n        if(mp.size() == 1) {\n            int pos = mp.begin()->first;\n            if(pos == 0 || (pos < (n - pos))) {\n                mp[n-1] = 1;\n                return (n - 1);\n            }\n            if((pos == n - 1) || (pos >= (n - pos))) {\n                mp[0] = 1;\n                return 0;\n            }\n        }\n\n        int left = mp.begin()->first, right = mp.rbegin()->first;\n        int maxi = left, ind = 0;\n        auto it = mp.begin();\n        while(it != mp.end()) {\n            // If rightmost student is encountered and he is not at (n - 1) position, then check if placing the student at (n - 1) will maximize the distance or not.\n            if(it->first == right) {\n                if(it->first != (n - 1) && (n - 1 - it->first) > maxi) {\n                    ind = n - 1;\n                }\n                break;\n            }\n\n            // Check if student can be placed between current and next placed student.\n            ++it;\n            int nxt = it->first;\n            --it;\n            if((nxt - it->first) / 2 > maxi) {\n                maxi = (nxt - it->first) / 2;\n                ind = it->first + (nxt - it->first) / 2;\n            }\n            ++it;\n        }\n        mp[ind] = 1;\n        return ind;\n    }\n    \n    void leave(int p) {\n        mp.erase(p);\n    }\n};\n\n/**\n * Your ExamRoom object will be instantiated and called as such:\n * ExamRoom* obj = new ExamRoom(n);\n * int param_1 = obj->seat();\n * obj->leave(p);\n */\n```
['Ordered Map', 'C++']