3355. Zero Array Transformation I LeetCode Solution

In this guide, you will get 3355. Zero Array Transformation I LeetCode Solution with the best time and space complexity. The solution to Zero Array Transformation I 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

  1. Problem Statement
  2. Complexity Analysis
  3. Zero Array Transformation I solution in C++
  4. Zero Array Transformation I solution in Java
  5. Zero Array Transformation I solution in Python
  6. Additional Resources
3355. Zero Array Transformation I LeetCode Solution image

Problem Statement of Zero Array Transformation I

You are given an integer array nums of length n and a 2D array queries, where queries[i] = [li, ri].
For each queries[i]:

Select a subset of indices within the range [li, ri] 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.

Example 1:

Input: nums = [1,0,1], queries = [[0,2]]
Output: true
Explanation:

For i = 0:

Select the subset of indices as [0, 2] and decrement the values at these indices by 1.
The array will become [0, 0, 0], which is a Zero Array.

Example 2:

Input: nums = [4,3,2,1], queries = [[1,3],[0,2]]
Output: false
Explanation:

For i = 0:

Select the subset of indices as [1, 2, 3] and decrement the values at these indices by 1.
The array will become [4, 2, 1, 0].

See also  1307. Verbal Arithmetic Puzzle LeetCode Solution

For i = 1:

Select the subset of indices as [0, 1, 2] and decrement the values at these indices by 1.
The array will become [3, 1, 0, 0], which is not a Zero Array.

Constraints:

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

3355. Zero Array Transformation I LeetCode Solution in C++

class Solution {
 public:
  bool isZeroArray(vector<int>& nums, vector<vector<int>>& queries) {
    vector<int> line(nums.size() + 1);
    int decrement = 0;

    for (const vector<int>& query : queries) {
      const int l = query[0];
      const int r = query[1];
      ++line[l];
      --line[r + 1];
    }

    for (int i = 0; i < nums.size(); ++i) {
      decrement += line[i];
      if (decrement < nums[i])
        return false;
    }

    return true;
  }
};
/* code provided by PROGIEZ */

3355. Zero Array Transformation I LeetCode Solution in Java

class Solution {
  public boolean isZeroArray(int[] nums, int[][] queries) {
    int[] line = new int[nums.length + 1];
    int decrement = 0;

    for (int[] query : queries) {
      final int l = query[0];
      final int r = query[1];
      ++line[l];
      --line[r + 1];
    }

    for (int i = 0; i < nums.length; ++i) {
      decrement += line[i];
      if (decrement < nums[i])
        return false;
    }

    return true;
  }
}
// code provided by PROGIEZ

3355. Zero Array Transformation I LeetCode Solution in Python

class Solution:
  def isZeroArray(self, nums: list[int], queries: list[list[int]]) -> bool:
    line = [0] * (len(nums) + 1)
    decrement = 0

    for l, r in queries:
      line[l] += 1
      line[r + 1] -= 1

    for i, num in enumerate(nums):
      decrement += line[i]
      if decrement < num:
        return False

    return True
# code by PROGIEZ

Additional Resources

See also  1356. Sort Integers by The Number of 1 Bits LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.