Skip to main content
Back to problems
Leetcode
Medium
Arrays
Greedy
Google
Zero Array Transformation I

Determine whether an array can be transformed into all zeros using a fixed set of range decrement operations.

Acceptance 100%
Problem Statement

Zero Array Transformation I

You are given an integer array nums and a list of queries. Each query describes a contiguous subarray range. For a chosen query, you may apply one operation that decreases every element in that range by 1.

Your task is to determine whether it is possible to apply some subset of the queries so that, after all chosen operations, every value in nums becomes exactly 0.

The key constraint is that a query can be used at most once, and if an element is decremented multiple times, all decrements must be supported by queries whose ranges include that position.

Return true if the transformation is possible, otherwise return false.

Notes

  • You do not need to output the actual set of queries.
  • The order of applying chosen queries does not matter.
  • This is a feasibility problem: decide whether enough coverage exists at every index to reduce the array to zero exactly.

Input Format

  • nums: an integer array.
  • queries: an array of ranges, where each range is typically represented by two indices [l, r] inclusive.

The exact platform input format may vary, but the core objects are:

  1. The target array nums
  2. A list of inclusive subarray ranges that may be used for decrement operations

Output Format

Return a boolean value:

  • true if nums can be transformed into all zeros
  • false otherwise

Constraints

  • Array length and number of queries are finite and suitable for algorithmic processing.
  • Ranges are inclusive and valid indices into nums.
  • All decrement operations reduce values by exactly 1 over the selected range.

Common intended constraints for this type of problem are large enough that an O(nq)O(nq) simulation may be too slow, so an efficient range-accumulation approach is expected.

Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [2,1,2]
queries = [[0,1],[1,2],[0,2]]

Output

true

Explanation

Use the first and third queries to decrement index 0 twice, the first and second queries to decrement index 1 twice, and the second and third queries to decrement index 2 twice overall as needed by the array values. The ranges provide enough coverage to reduce every element to zero.

Example 2

Input

nums = [1,2,1]
queries = [[0,0],[2,2]]

Output

false

Explanation

Index 1 needs two decrements, but no query covers it. Therefore the array cannot be transformed into all zeros.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.