Optimally select the query using Priority Queue
Intuitionif nums[i] is greater than the queries under consideration , then we need to consider more queries .Approachhow to consider queries ?
we will sort them
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
if nums[i] is greater than the queries under consideration , then we need to consider more queries .

# Approach
<!-- Describe your approach to solving the problem. -->
how to consider queries ?
1. we will sort them according to the starting index
2. create eligible priority_queue which can be considered if nums[i] is greater than the queries under consideration
3. now select the query from the eligible queries optimally i.e. query with max ending index
4. keep track of queries getting considered substract the count from the no of provided queries
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
max(O(n),O(q)) as we are traversing the nums and query only once
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(q) as we are storing the queries in the worst case will store all queries at once .
# Code
```cpp []
class Solution {
public:
    int maxRemoval(vector<int>& nums, vector<vector<int>>& q) {
        // [0,3],[0,2],[1,5],[1,4]
        int n=nums.size();
        int m=q.size();
        sort(q.begin(),q.end());
        priority_queue<pair<int,int>>eligible; // store the eligible queries
        priority_queue<int,vector<int>,greater<int>>pq; //keep the minimum queries 
        int i=0;// iterate the nums
        int j=0; // itereate the q
        int ans=0;
        while(i<n){
            // cout<<i<<endl;
            if(nums[i]==0) {
                while(pq.size()&&i==pq.top()) pq.pop();
                i++;
                continue;
            }
                
            while(nums[i]>pq.size()){
                
                // cout<<"j :"<<j<<endl;
                while(j<m&&q[j][0]<=i){
                    // cout<<"element of pushed :"<<q[j][0]<<"  "<<q[j][1]<<endl;
                    eligible.push({q[j][1],q[j][0]});
                    j++;
                }
                if(eligible.size()){
                    auto top=eligible.top();
                    // cout<<"top element of eligible :"<<top.first<<"  "<<top.second<<endl;
                    if(i<=top.first){
                        // cout<<"pushed to pq :"<<top.first<<endl;
                        pq.push(top.first);
                        ans+=1;
                    }
                    // cout<<"current max :"<<pq.size()<<endl;
                    eligible.pop();
                }
                else return -1;
                // cout<<"nums[i] :"<<nums[i]<<"  "<<pq.size()<<endl;
            }
            if(nums[i]>pq.size()) {return -1;}
            while(pq.size()&&i>=pq.top()) pq.pop();
            i++;

        }
        return m-ans;
    }
};
```
['Sorting', 'Heap (Priority Queue)', 'C++']