Apply Operations to Make All Array Elements Equal to Zero - Problem

You are given a 0-indexed integer array nums and a positive integer k.

Your goal is to make all array elements equal to zero by applying a special operation any number of times:

  • Choose any subarray of size k from the array
  • Decrease all elements in that subarray by 1

Return true if you can make all array elements equal to 0, or false otherwise.

Note: A subarray is a contiguous non-empty part of an array.

Example: For array [2,2,3,1] with k=2, you could apply the operation on subarray [2,3] (indices 1-2) to get [2,1,2,1], then continue until all elements become zero.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [2,2,3,1], k = 2
โ€บ Output: true
๐Ÿ’ก Note: We can apply operations: [2,2,3,1] โ†’ [1,1,3,1] โ†’ [0,0,3,1] โ†’ [0,0,2,0] โ†’ [0,0,1,0] โ†’ [0,0,0,0]. Each operation reduces k=2 consecutive elements by 1.
example_2.py โ€” Impossible Case
$ Input: nums = [1,3,1,1], k = 2
โ€บ Output: false
๐Ÿ’ก Note: No matter how we apply operations, we cannot make all elements zero. The middle element 3 creates an imbalance that cannot be resolved with k=2.
example_3.py โ€” Edge Case
$ Input: nums = [1,2,1], k = 3
โ€บ Output: true
๐Ÿ’ก Note: With k=3, we can apply the operation on the entire array twice: [1,2,1] โ†’ [0,1,0] โ†’ [0,1,0]. Wait, we need one more operation but can't apply it. Actually this should be false.

Visualization

Tap to expand
Water Tank Drainage SystemTank 1: 2LTank 2: 2LTank 3: 3LTank 4: 1LDrainage Tool (k=2)Greedy Strategy:1. Start from leftmost tank2. Drain enough to empty current tank3. Track effect on next k-1 tanks4. Move to next positionAlgorithm Steps:โ€ข Process tanks[0]: needs 2 drains โ†’ diff = -2โ€ข Process tanks[1]: 2 + (-2) = 0 โœ“โ€ข Process tanks[2]: 3 + (-2) = 1, need 1 drain โ†’ diff = -3โ€ข Check tanks[3]: 1 + (-3) = -2 โœ— IMPOSSIBLEResult: FALSE (over-drained)
Understanding the Visualization
1
Setup
We have tanks with different water levels and a drainage tool
2
Greedy Processing
Process from left to right, drain as needed
3
Track Effects
Each drainage affects k consecutive tanks
4
Verify Result
Check if all tanks can be emptied
Key Takeaway
๐ŸŽฏ Key Insight: Process greedily from left to right, tracking cumulative operations. If any position becomes negative, the solution is impossible.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the array

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for counters

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 0 โ‰ค nums[i] โ‰ค 106
  • 1 โ‰ค k โ‰ค nums.length
  • All operations must use subarrays of exactly size k
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
23.8K Views
Medium Frequency
~25 min Avg. Time
987 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen