Beginner Friendly Solution using PriorityQueue
\nclass Solution {\n    public int maxRemoval(int[] nums, int[][] queries) {\n        int count = 0, n = nums.length, j = 0;\n        \n        // To track, Ava
```\nclass Solution {\n    public int maxRemoval(int[] nums, int[][] queries) {\n        int count = 0, n = nums.length, j = 0;\n        \n        // To track, Available Queries - sorted in Descending order, we greedily pick farthest end point\n        PriorityQueue<Integer> availableQueries = new PriorityQueue<>(Collections.reverseOrder()); //  It stores ending point of a query\n        \n        // To track the Used Queries so far & check its effect is Valid or not\n        PriorityQueue<Integer> usedQueries = new PriorityQueue<>(); //  It stores ending point of a query\n        \n        // Sorting Queries, so that we can quickly find & add queries available from current index\n        Arrays.sort(queries, (a,b) -> a[0] - b[0]);\n        \n        // To keep track of effect of the Query\n        int effect = 0;\n        \n        \n        for(int i=0;i<n;i++)\n        {\n            \n            // Picking Queries starting from this index & adding it to Available Queries list, from which queries may or may not be used\n            while(j < queries.length && queries[j][0] == i)\n            {\n                availableQueries.add(queries[j][1]);\n                j++;\n            }\n                \n            \n            // Removing the effect of Earlier Used Queries, as it is not Valid at this point, if their ending point of those queries are earlier than this index\n            while(!usedQueries.isEmpty() && usedQueries.peek() < i)\n            {\n                effect++; // to negate the effect of the query\n                usedQueries.poll(); // removing the query from used list, so we can check whether next query endpoint is Valid or not.\n            }\n            \n            \n            // Check whether nums[i] + the effect of Used Valid Queries so far and still its greater than zero, if so we can try adding few more queries which is available \n            while(nums[i] + effect > 0 && !availableQueries.isEmpty() && availableQueries.peek() >= i) \n            {\n                usedQueries.add(availableQueries.poll()); // Greedily picking farthest end point query from Available Valid Queries & adding it to Used Queries list\n                count++; // we used one more query, so adding it\n                effect--; // we used one more query, so counting its effect\n                if(!availableQueries.isEmpty() && availableQueries.peek() < i) // while removing the queries, we might encounter queries which has endpoint less than current index, so no point in having them further, so clear it.\n                    availableQueries.clear();\n                \n            }\n            \n            \n            if(nums[i] + effect > 0) // check even after processing, we are not able to bring it down to zero or lesser\n                return -1;\n            \n        }\n       \n        return queries.length - count; // finally our ans will be Total Queries Length - Required Queries\n        \n    }\n}\n\n```\n\nTime Complexity : O(N log(q)) where N = nums length and Q = Queries length\nSpace Complexity : O(Q)
['Java']