Greedy approach
Approach\n\n## Step 1: Preprocess Queries\n\nThe queries are sorted by their starting index ($l_i$) to ensure they are processed in increasing order of their ap
# Approach\n\n## Step 1: Preprocess Queries\n\nThe queries are sorted by their starting index ($l_i$) to ensure they are processed in increasing order of their application range. This allows the program to efficiently add queries to the "available" queue ($availableEndIndices$) as it processes each index in $nums$.\n\n## Step 2: Manage Queries with Heaps\n\nThe code uses two priority queues to handle queries:\n\n- $availableEndIndices$ (Max-Heap):\n  - This heap contains the end indices ($r_i$) of queries that could potentially be used at the current position in $nums$.\n  - Queries are added to this heap when their starting index ($l_i$) matches the current position being processed.\n  - The largest $r_i$ is at the top, ensuring that the code prioritizes the longest-range queries first, which are more likely to remain useful in subsequent steps.\n\n- $activeEndIndices$ (Min-Heap):\n  - This heap contains the end indices ($r_i$) of queries currently being used to decrement $nums$.\n  - These queries are removed from the heap when their range expires (i.e., when the current index matches their end index).\n\n## Step 3: Process Each Position in $nums$\n\n- For each position $i$ in $nums$, the value of $nums[i]$ is first reduced by the size of the $activeEndIndices$ heap, as each active query contributes a decrement of $1$ to $nums[i]$.\n- If $nums[i] > 0$ after this adjustment, the program attempts to use queries from $availableEndIndices$:\n  - The largest available query (based on $r_i$) is moved to $activeEndIndices$ and contributes to decrementing $nums[i]$.\n  - This process continues until $nums[i]$ is reduced to $0$ or no valid queries remain.\n- If $nums[i] > 0$ and no further queries can reduce it, the function returns $-1$ (it\'s impossible to make nums a zero array).\n\n## Step 4: Handle Expired Queries\n\nAfter processing $nums[i]$, queries in $activeEndIndices$ that expire at the current position ($i$) are removed from the heap. This ensures that only relevant queries remain in $activeEndIndices$, keeping the decrement count accurate for subsequent positions.\n\n## Step 5: Count Unused Queries\n\nThe total number of unused queries is calculated as:\n\n    unused queries=total queries\u2212successful queries\n\nThis value is returned as the result.\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int maxRemoval(vector<int>& nums, vector<vector<int>>& queries) {\n        // Priority queues to manage query processing\n        priority_queue<int> availableEndIndices; // Max-heap for candidate queries\' end indices\n        priority_queue<int, vector<int>, greater<>> activeEndIndices; // Min-heap for active queries\' end indices\n\n        // Sort queries by their starting indices\n        sort(queries.begin(), queries.end());\n\n        int successfulQueries = 0; // Count of queries successfully used\n        int queryPointer = 0;      // Tracks the current query being processed\n\n        // Process each position in nums\n        for (int index = 0; index < nums.size(); ++index) {\n            // Add all queries starting at the current index to the available queue\n            while (queryPointer < queries.size() && queries[queryPointer][0] == index) {\n                availableEndIndices.push(queries[queryPointer][1]);\n                ++queryPointer;\n            }\n\n            // Adjust the current number by subtracting the count of active queries\n            nums[index] -= activeEndIndices.size();\n\n            // Use candidate queries to fulfill the current position\'s requirement\n            while (nums[index] > 0 && !availableEndIndices.empty() && availableEndIndices.top() >= index) {\n                ++successfulQueries;\n                activeEndIndices.push(availableEndIndices.top()); // Move query from available to active\n                availableEndIndices.pop();\n                --nums[index];\n            }\n\n            // If the current position cannot be zeroed out, return -1\n            if (nums[index] > 0)\n                return -1;\n\n            // Remove expired queries from the active queue\n            while (!activeEndIndices.empty() && activeEndIndices.top() == index)\n                activeEndIndices.pop();\n        }\n\n        // Return the number of queries that can be removed\n        return queries.size() - successfulQueries;\n    }\n};\n\n```
['C++']