Simple sorting approach
Intuition\nFirst sort both queries and rooms in descending order of room size. Iterate over queries. As you iterate, keep adding rooms that satisfy the query re
# Intuition\nFirst sort both queries and rooms in descending order of room size. Iterate over queries. As you iterate, keep adding rooms that satisfy the query requirements to a set. The advantage here is that since we have sorted in descending order, if a room satisfies `query[i]` it will surely satisfy `query[i+1]`, since the bar decreases as we move forward. So we make only a single pass over the `rooms` array. \n\nAfter the rooms satisfying a query have been added, pick out elements from the set, and put the best one for the query into the final answer.\n\n \n# Approach\nSort both arrays in descending order. Traverse query array, and keep populating the set of rooms. Pick out two possibilities. \n\n1. Room number which is closest, but greater than or equal to asked room number. \n2. Room number which is closest, but lesser than asked room number.\n\nConsider both when evaluating final answer.\n\n# Complexity\n- Time complexity:\nO(nlogn)\n\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution {\npublic:\n\n    struct sorter {\n        bool operator()(vector<int>&a, vector<int>&b){\n            if(a[1]==b[1])\n                return a[0] > b[0];\n            return a[1] > b[1];\n        }\n    };\n\n    vector<int> closestRoom(vector<vector<int>>& rooms, vector<vector<int>>& queries) {\n       \n       for(int i=0;i<rooms.size();i++){\n           rooms[i].push_back(i);\n       }\n\n       for(int i=0;i<queries.size();i++){\n           queries[i].push_back(i);\n       }\n       \n       sort(rooms.begin(), rooms.end(), sorter());\n       sort(queries.begin(), queries.end(), sorter());\n\n       set<pair<int,int>>s;\n\n       int cur=0;\n\n       vector<int>ans(queries.size(), -1);\n\n       for(int i=0;i<queries.size();i++){\n           while(cur < rooms.size()){\n               if(rooms[cur][1]>=queries[i][1]){\n                   int roomId=rooms[cur][0];\n                   int roomSize=rooms[cur][1];\n                   s.insert({roomId, roomSize});\n                }\n               else {\n                   break;\n               }\n               cur++;\n           }\n           auto it=s.lower_bound({queries[i][0], 0});\n           if(it!=s.end()){\n               ans[queries[i][2]] = it->first;               \n           }\n           if(it!=s.begin()){\n                it--;\n                \n                if(ans[queries[i][2]] == -1){\n                    ans[queries[i][2]] = it->first;\n                }\n                else {\n                    int curRoom = ans[queries[i][2]];\n                    int askedRoom = queries[i][0];\n                    int newRoom = it->first;\n                    if(abs(newRoom-askedRoom) <= abs(curRoom-askedRoom)){\n                        if(abs(newRoom-askedRoom) == abs(curRoom-askedRoom)){\n                            ans[queries[i][2]] = min(curRoom, newRoom);\n                        }\n                        else {\n                            ans[queries[i][2]] = newRoom;\n                        }\n                    }\n                }\n                \n            }\n       }\n        return ans;\n\n    }\n};\n```
['C++']