DEV Community

Cover image for 3355. Zero Array Transformation I
MD ARIFUL HAQUE
MD ARIFUL HAQUE

Posted on

3355. Zero Array Transformation I

3355. Zero Array Transformation I

Difficulty: Medium

Topics: Array, Prefix Sum

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 subset1 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].
    • 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

Hint:

  1. Can we use difference array and prefix sum to check if an index can be made zero?

Solution:

We need to determine if it's possible to transform an array into a zero array by processing a series of queries. Each query allows us to select a subset of indices within a specified range and decrement their values by 1. The goal is to check if we can apply these queries in such a way that all elements in the array become zero.

Approach

  1. Difference Array for Range Updates: We use a difference array to efficiently compute how many times each index is covered by the queries. This helps us determine the maximum number of times each element can be decremented.
  2. Check Coverage: For each element in the array, we ensure that the number of queries covering it is at least as large as the element's value. If any element is covered by fewer queries than its value, it's impossible to reduce it to zero.
  3. Sum Check: We also check if the total number of decrements required (sum of all elements) does not exceed the maximum possible decrements allowed by the queries. This is calculated as the sum of the lengths of all query ranges.

Let's implement this solution in PHP: 3355. Zero Array Transformation I

<?php
/**
 * @param Integer[] $nums
 * @param Integer[][] $queries
 * @return Boolean
 */
function isZeroArray($nums, $queries) {
        ...
    ...
    ...
    /**
     * go to ./solution.php
     */
}

// Example usage:
$nums1 = [1, 0, 1];
$queries1 = [[0, 2]];
var_dump(isZeroArray($nums1, $queries1)); // true

$nums2 = [4, 3, 2, 1];
$queries2 = [[1, 3], [0, 2]];
var_dump(isZeroArray($nums2, $queries2)); // false
?>
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Difference Array: We use a difference array to track the start and end of each query's range. This allows us to efficiently compute the number of times each index is covered by the queries using a prefix sum.
  2. Coverage Check: For each element, we check if the number of times it is covered by queries (stored in Q[i]) is at least its value. If not, transforming it to zero is impossible.
  3. Sum Check: We ensure the total number of required decrements (sum of all elements) does not exceed the maximum possible decrements (sum of all query range lengths). This ensures that even if we use all possible decrements in each query, we can meet the required total.

By combining these checks, we efficiently determine if transforming the array into a zero array is feasible.

Contact Links

If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:


  1. Subset: A subset of an array is a selection of elements (possibly none) of the array

Top comments (0)