[C++] | Circular Array | Ordered Set | Priority Queue | O(N (log K + log N) | O(K)
APPROACH\n> Do what it says\n\n## TECHNIQUE\n> We use the popular technique for circular arrays making it a linear array (by copying itself) \n\nnew_servers = 0
## **APPROACH**\n> Do what it says\n\n## **TECHNIQUE**\n> We use the popular technique for circular arrays making it a linear array (by copying itself) \n\nnew_servers = `0` to `2*k - 1`\nwhere `i` and `i + k`th servers are the same\nSo whenever we change the state of server we do it for both `i` and `i + k`\n\n**FOR NEXT FREE SERVER** after `i % k` \nor \nsmallest free server greater than `i` (circularly), we can do `free_servers.lower_bound(i%k) % k` \nlast `%k` because `x = i_th` or `x = (i + k)_th` server map to the same `(x % k)_th server`\n\nFor freeing the servers you can use a `min_heap`\n\n## **TIME COMPLEXITY**\n`O(N * (log K + log N))`\n\n## **SPACE COMPLEXITY**\n`O(K + N)`\n\n**Note** There are better and cleaner solutions than this, you can still learn about the circular array technique which can be used on some other problems :)\n\n### **The Technique**\nCircular array `[1, 2, 3, ... K]`\nLinear array `[1, 2, 3, ... K, 1, 2, 3 ... K-1]`\nfor this array each subarray of length K is a rotation of the circular array\n\n## **CODE CPP**\n\n```cpp\nclass Solution {\npublic:\n    vector<int> busiestServers(int k, vector<int>& arr, vector<int>& lo) {\n        set<int> free;\n        int n = arr.size();\n        for (int i = 0; i < 2*k - 1; i ++)\n            free.insert(i);\n        vector<int> cnt(k, 0);\n        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;\n        for (int i = 0; i < n; i ++)\n        {\n            int at = arr[i];\n            int load = lo[i];\n            while (!pq.empty() and pq.top().first <= at)\n            {\n                auto tt = pq.top();\n                int server = tt.second;\n                pq.pop();\n                free.insert(server);\n                free.insert(server + k);\n            }\n            if (!free.empty()) {\n                int server = (*free.lower_bound(i % k)) % k;\n                free.erase(server % k);\n                free.erase(server % k + k);\n\n                cnt[server] ++;\n                pq.push({at + load, server});\n            }\n        }\n        int mv = -1;\n        vector<int> res;\n        for (int i = 0; i < k; i ++)\n        {\n            if (cnt[i] > mv)\n            {\n                mv = cnt[i];\n                res = {i};\n            }\n            else if (cnt[i] == mv)\n                res.push_back(i);\n        }\n        return res;\n    }\n};\n```
[]