Zero Array Transformation III - Problem
Zero Array Transformation III challenges you to find the maximum number of queries you can remove while still being able to transform an array to all zeros.

You're given an integer array nums and a 2D array queries where each queries[i] = [li, ri] represents a range operation. Each query allows you to decrement any values in the range [li, ri] by at most 1 (you can choose how much to decrement each position independently, from 0 to 1).

Your goal: Remove as many queries as possible while ensuring the remaining queries can still transform nums into a zero array (all elements equal to 0). Return the maximum number of removable queries, or -1 if it's impossible even with all queries.

Key insight: This is an optimization problem where you need to find the minimum set of queries required, then subtract from the total to get the maximum removals.

Input & Output

example_1.py — Basic Case
$ Input: nums = [2,0,2], queries = [[0,2],[1,2]]
Output: 0
💡 Note: We need both queries to make the array zero. Query [0,2] can reduce positions 0 and 2 by 1 each (making them [1,0,1]), then query [1,2] reduces positions 1 and 2 by 1 each (making them [1,0,0]). We still need query [0,2] again to reduce position 0 to 0, but we can reuse queries. Actually, each query can be applied optimally to exactly cover what's needed.
example_2.py — Impossible Case
$ Input: nums = [1,1,1,1], queries = [[1,3]]
Output: -1
💡 Note: The single query [1,3] can only cover positions 1, 2, and 3, but position 0 has value 1 and cannot be covered by any query. Therefore, it's impossible to make all elements zero.
example_3.py — Multiple Solutions
$ Input: nums = [1,0,1], queries = [[0,0],[2,2],[0,2]]
Output: 2
💡 Note: We can use just query [0,2] to cover both positions 0 and 2, reducing them to 0. The other two queries [0,0] and [2,2] are redundant and can be removed. Maximum removals = 2.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 0 ≤ nums[i] ≤ 105
  • 1 ≤ queries.length ≤ 105
  • queries[i].length == 2
  • 0 ≤ li ≤ ri < nums.length

Visualization

Tap to expand
Zero Array Transformation III INPUT nums array: 2 [0] 0 [1] 2 [2] queries: Q0: [0, 2] covers all Q1: [1, 2] idx 1-2 Query Coverage: Q0: [0,2] Q1: [1,2] GREEDY ALGORITHM 1 Sort by end index Process queries greedily 2 Track coverage needs nums[0]=2, nums[2]=2 3 Select minimum queries Both Q0 and Q1 needed 4 Count removable total - required = 0 Coverage Analysis Index 0: needs 2 hits Q0 covers: 1 hit Still need: 1 more Index 2: needs 2 hits Q0+Q1 cover: 2 hits OK - exactly enough! FINAL RESULT Target: Zero Array 0 0 0 Queries Used: 2/2 All queries required! OUTPUT 0 Maximum removable queries = 0 Cannot remove any query Both are essential Key Insight: Greedy approach: Process positions left-to-right, selecting queries that extend coverage furthest. For nums=[2,0,2], index 0 needs 2 decrements but only Q0 covers it. Index 2 also needs 2 hits, requiring both Q0 and Q1. With all queries essential, we can remove 0 queries. TutorialsPoint - Zero Array Transformation III | Greedy Approach
Asked in
Google 35 Meta 28 Amazon 22 Microsoft 18
43.7K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen