Java Solution based on treeset
Idea is to Use a TreeSet to store the current seat index in use. Whenever we want to add a seat, check the max distance adjacent indices in the treeset, whichev
Idea is to Use a [TreeSet](https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html) to store the current seat index in use. Whenever we want to add a seat, check the max distance adjacent indices in the treeset, whichever is the max that is our answer.\nFor edge cases : \nWhen zero student: return 0;\nWhen there is one student., return 0 if the student is in second half or return N - 1 if the student is in first half.\nOtherwise, apply the idea,\n```\nclass ExamRoom {\n\n    TreeSet<Integer> s = new TreeSet<>();\n    int N;\n\n    public ExamRoom(int N) {\n        this.N = N;\n    }\n\n    public int seat() {\n\t//When no student\n        if (s.isEmpty()) {\n            s.add(0);\n            return 0;\n        }\n\t//When One student\n        if (s.size() == 1) {\n            int num = s.first();\n            if (num < (N / 2)) {\n                s.add(N - 1);\n                return N - 1;\n            } else {\n                s.add(0);\n                return 0;\n            }\n        }\n\t//When more than one student\n        Iterator<Integer> it = s.iterator();\n        int dist = -1;\n        int position = -1;\n        int left = it.next();\n\t//check the distance between 0 and first student\n        if (left > 0) {\n            position = 0;\n            dist = left;\n        }\n        int right = -1;\n\t//Check the distance between adjacent indices,(already sorted)\n        while (it.hasNext()) {\n            right = it.next();\n            if ((right - left) / 2 > dist) {\n                dist = (right - left) / 2;\n                position = left + dist;\n            }\n            left = right;\n        }\n\t//check the distance between last student and (N - 1)\n        if ((N - 1) - left > dist) {\n            position = N - 1;\n        }\n        s.add(position);\n        return position;\n    }\n\n    public void leave(int p) {\n        s.remove(p);\n    }\n}\n```
[]