Mid/SeniorArrayPrefix Sum

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
Inputnums = [1,0,1], queries = [[0,2]]
Outputtrue
Selecting indices 0 and 2 for the only query decrements both 1s, making the array [0, 0, 0].
Example 2
Inputnums = [4,3,2,1], queries = [[1,3],[0,2]]
Outputfalse
Even after applying both queries, the array can become [3, 1, 0, 0], which is not a Zero Array.

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^5
  • 1 <= queries.length <= 10^5
  • queries[i].length == 2
  • 0 <= li <= ri < nums.length

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate