Binary Search | Sorting | Without Set
Complexity
Time complexity:
O(nlogn + m* (logn + n) ) where n:rooms.length , m=queries.length
Space complexity:
O(1) if not considered answer array which we hav

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
O(nlogn + m* (logn + n) ) where n:rooms.length , m=queries.length
- Space complexity:
O(1) if not considered answer array which we have to return

# Code
```cpp []
class Solution {
public:
    int findIndex(int num,vector<vector<int>>&rooms){
        int n=rooms.size();
        int l=0,r=n-1;
        int index=-1;
        while(l<=r){
            int mid=(l+r)/2;
            if(rooms[mid][1]<num){
                l=mid+1;
            }
            else{
                index=mid;
                r=mid-1;
            }
        }
        return index;
    }
    int findIndex_(int ind,vector<vector<int>>&rooms,int id){
        int ans=ind;
        int mini=abs(rooms[ind][0]-id);
        for(int i=ind+1;i<rooms.size();i++){
            int diff=abs(rooms[i][0]-id);
            if(diff<mini){
                mini=diff;
                ans=i;
            }else if(diff==mini){
                if(rooms[i][0]<rooms[ans][0]){
                    ans=i;
                }
            }
        }
        return ans;  
    }
    vector<int> closestRoom(vector<vector<int>>& rooms, vector<vector<int>>& queries) {
        //sort rooms according to size
        sort(rooms.begin(),rooms.end(),[](vector<int>&a,vector<int>&b){
            if(a[1]<b[1] || (a[1]==b[1] && a[0]<b[0])){
                return true;
            }
            return false;
        });
        for(auto i : rooms){
            cout<<i[0]<<" "<<i[1]<<endl;
        }
        int maxSize=rooms[rooms.size()-1][1];
        vector<int>res;
        for(auto i : queries){
            int id=i[0],minSize=i[1];
            if(maxSize<minSize){
                res.push_back(-1);
                continue;
            }
            int index1=findIndex(minSize,rooms);
            // cout<<index1<<endl;
            int index2=findIndex_(index1,rooms,id);
            res.push_back(rooms[index2][0]);  
        }
        return res;
    }
};
```
['Array', 'Binary Search', 'Sorting', 'C++']