3356. Zero Array Transformation II LeetCode Solution
In this guide, you will get 3356. Zero Array Transformation II LeetCode Solution with the best time and space complexity. The solution to Zero Array Transformation II problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Zero Array Transformation II solution in C++
- Zero Array Transformation II solution in Java
- Zero Array Transformation II solution in Python
- Additional Resources

Problem Statement of Zero Array Transformation II
You are given an integer array nums of length n and a 2D array queries where queries[i] = [li, ri, vali].
Each queries[i] represents the following action on nums:
Decrement the value at each index in the range [li, ri] in nums by at most vali.
The amount by which each value is decremented can be chosen independently for each index.
A Zero Array is an array with all its elements equal to 0.
Return the minimum possible non-negative value of k, such that after processing the first k queries in sequence, nums becomes a Zero Array. If no such k exists, return -1.
Example 1:
Input: nums = [2,0,2], queries = [[0,2,1],[0,2,1],[1,1,3]]
Output: 2
Explanation:
For i = 0 (l = 0, r = 2, val = 1):
Decrement values at indices [0, 1, 2] by [1, 0, 1] respectively.
The array will become [1, 0, 1].
For i = 1 (l = 0, r = 2, val = 1):
Decrement values at indices [0, 1, 2] by [1, 0, 1] respectively.
The array will become [0, 0, 0], which is a Zero Array. Therefore, the minimum value of k is 2.
Example 2:
Input: nums = [4,3,2,1], queries = [[1,3,2],[0,2,1]]
Output: -1
Explanation:
For i = 0 (l = 1, r = 3, val = 2):
Decrement values at indices [1, 2, 3] by [2, 2, 1] respectively.
The array will become [4, 1, 0, 0].
For i = 1 (l = 0, r = 2, val = 1):
Decrement values at indices [0, 1, 2] by [1, 1, 0] respectively.
The array will become [3, 0, 0, 0], which is not a Zero Array.
Constraints:
1 <= nums.length <= 105
0 <= nums[i] <= 5 * 105
1 <= queries.length <= 105
queries[i].length == 3
0 <= li <= ri < nums.length
1 <= vali <= 5
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
3356. Zero Array Transformation II LeetCode Solution in C++
class Solution {
public:
int minZeroArray(vector<int>& nums, vector<vector<int>>& queries) {
vector<int> line(nums.size() + 1);
int decrement = 0;
int k = 0;
for (int i = 0; i < nums.size(); ++i) {
while (decrement + line[i] < nums[i]) {
if (k == queries.size())
return -1;
const int l = queries[k][0];
const int r = queries[k][1];
const int val = queries[k][2];
++k;
if (r < i)
continue;
line[max(l, i)] += val;
line[r + 1] -= val;
}
decrement += line[i];
}
return k;
}
};
/* code provided by PROGIEZ */
3356. Zero Array Transformation II LeetCode Solution in Java
class Solution {
public int minZeroArray(int[] nums, int[][] queries) {
int[] line = new int[nums.length + 1];
int decrement = 0;
int k = 0;
for (int i = 0; i < nums.length; ++i) {
while (decrement + line[i] < nums[i]) {
if (k == queries.length)
return -1;
final int l = queries[k][0];
final int r = queries[k][1];
final int val = queries[k][2];
++k;
if (r < i)
continue;
line[Math.max(l, i)] += val;
line[r + 1] -= val;
}
decrement += line[i];
}
return k;
}
}
// code provided by PROGIEZ
3356. Zero Array Transformation II LeetCode Solution in Python
class Solution:
def minZeroArray(self, nums: list[int], queries: list[list[int]]) -> int:
line = [0] * (len(nums) + 1)
decrement = 0
k = 0
for i, num in enumerate(nums):
while decrement + line[i] < num:
if k == len(queries):
return -1
l, r, val = queries[k]
k += 1
if r < i:
continue
line[max(l, i)] += val
line[r + 1] -= val
decrement += line[i]
return k
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.