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.

You can apply the following operation on the array any number of times:

  • Choose any subarray of size k from the array and decrease all its elements by 1.

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

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

Input & Output

Example 1 — Basic Case
$ Input: nums = [2,1,3,4], k = 3
Output: true
💡 Note: Apply operation on [2,1,3] twice to get [0,0,1,4], then on [0,1,4] once to get [0,0,0,3], then on [0,0,3] three times to get [0,0,0,0]
Example 2 — Impossible Case
$ Input: nums = [1,3,1,1], k = 2
Output: false
💡 Note: Cannot make all elements zero because the last element cannot be part of any k-sized subarray when operations are constrained
Example 3 — Edge Case
$ Input: nums = [0,0,0], k = 1
Output: true
💡 Note: All elements are already zero, no operations needed

Constraints

  • 1 ≤ nums.length ≤ 105
  • 0 ≤ nums[i] ≤ 106
  • 1 ≤ k ≤ nums.length

Visualization

Tap to expand
Apply Operations: Make All Elements ZeroInput: nums = [2,1,3,4], k = 32134k=3 operationAfter operations:0000Goal: Apply operations to make all elements zeroOutput: true
Understanding the Visualization
1
Input
Array nums and subarray size k
2
Apply Operations
Choose k consecutive elements and decrease by 1
3
Check Result
Determine if all elements can become 0
Key Takeaway
🎯 Key Insight: Use prefix sum to track operation effects efficiently rather than simulating each step
Asked in
Google 15 Microsoft 12
18.0K Views
Medium Frequency
~15 min Avg. Time
450 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