Zero Array Transformation I - Problem

You are given an integer array nums of length n and a 2D array queries, where queries[i] = [l_i, r_i].

For each queries[i]:

  • Select a subset of indices within the range [l_i, r_i] in nums.
  • Decrement the values at the selected indices by 1.

A Zero Array is an array where all elements are equal to 0.

Return true if it is possible to transform nums into a Zero Array after processing all the queries sequentially, otherwise return false.

Input & Output

Example 1 — Basic Case
$ Input: nums = [2,1,3,4], queries = [[1,2],[1,3],[0,1]]
Output: false
💡 Note: Position 3 has value 4 but only 1 query can affect it (query [1,3]), so it cannot be reduced to 0
Example 2 — Possible Transformation
$ Input: nums = [1,0,1], queries = [[0,2]]
Output: true
💡 Note: Single query [0,2] can decrement positions 0 and 2 once each, making array [0,0,0]
Example 3 — Edge Case
$ Input: nums = [0,0,0], queries = []
Output: true
💡 Note: Array is already all zeros, no queries needed

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 ProblemInput Array:2134Queries:[1,2] can decrement pos 1,2[1,3] can decrement pos 1,2,3[0,1] can decrement pos 0,1Coverage Count:1221Position 3: needs 4 decrements, only 1 available → false
Understanding the Visualization
1
Input
Array nums and query ranges that can decrement positions
2
Process
Count how many queries can affect each position
3
Output
Check if coverage is sufficient to zero out all positions
Key Takeaway
🎯 Key Insight: Each position must have coverage count >= its value to be reducible to zero
Asked in
Google 12 Amazon 8 Microsoft 6 Meta 4
9.0K Views
Medium Frequency
~15 min Avg. Time
234 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