C++ Using Set -- Basic approach (No optimizations)
Intuition\n Describe your first thoughts on how to solve this problem. \nFirst approach one can think of. No algorithms, just implementation using appropriate D
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFirst approach one can think of. No algorithms, just implementation using appropriate DS.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nCalculating correct postion iteratively using brute force.\n\n<!-- # Complexity -->\n<!-- Let the number of seats occupied curretnly be m. -->\n<!-- - Time complexity: O(m) -->\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n<!-- - Space complexity: O(m) -->\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass ExamRoom {\nprivate:\n    set<int> S;\n    int n;\npublic:\n    ExamRoom(int n) {\n        this->n = n;\n    }\n    \n    int seat() {\n        int num_el = S.size();\n        if(num_el==n) {\n            return -1;\n        }\n        if(num_el==0) {\n            S.insert(0);\n            return 0;\n        }\n        if(num_el==1) { // add 0 or n-1 based on element present\n            int el = *S.begin();\n            if(el-0>n-1-el) {\n                S.insert(0);\n                return 0;\n            } else {\n                S.insert(n-1);\n                return n-1;\n            }\n        }\n        \n        int res = 0;\n        int cur_dif = -1;\n        for(auto itr=S.begin(); itr!=S.end(); itr++) {\n            if(next(itr)==S.end()) {\n                break;\n            }\n            //Calculate all potential positions using consecutive elements in the map\n            int mid = (*(next(itr)) + *itr)/2;\n            if(cur_dif<mid-(*itr)) {\n                cur_dif = mid-(*itr);\n                res = mid;\n            } \n        }\n\n        //Calculate ad-hoc if 0 or n-1 is absent in the map\n        if(S.find(0)==S.end()) {\n            if(*S.begin() >= cur_dif) {\n                S.insert(0);\n                return 0;\n            }\n        }\n        if(S.find(n-1)==S.end()) {\n            auto tmp = prev(S.end());\n            if(n-1-(*tmp) > cur_dif) {\n                S.insert(n-1);\n                return n-1;\n            }\n        }\n        S.insert(res);\n        return res;\n    }\n    \n    void leave(int p) {\n        S.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```
['C++']